[英]How to find the product and sum of digits of a number in java?
我需要找到輸入數字的數字的總和和乘積。 我有求和部分,但唯一的問題是,當我第一次輸入一個數字時,它給出了正確的答案,但是當我輸入另一個數字時,它只是將第一個數字的總和與第二個數字的總和相加第二個數字在一起,然后檢查兩個總和的總和是否為奇數(抱歉,如果它有點混亂)。 對於代碼的產品部分,我似乎無法弄清楚該怎么做。
最后,如果總和和乘積都是奇數,我需要說明輸入的數字是一個極奇數。
這是作業:
這個 Java 應用程序檢查區間 [101, 100001] 中的整數,看它們是否是“奇數”和“超級奇數”。
一個整數是“非常奇數”如果...
(0) 它是一個奇數 (1) 它有奇數個數字 (2) 它的所有數字都是奇數 (3) 它的數字之和是一個奇數 (4) 它的數字的乘積是一個奇數數字
“超級奇數”是一個非常奇數,這樣......
(5) 所有組成它的數位和的數位都是奇數 (6) 所有組成它的數位乘積的數位都是奇數
循環提示用戶輸入正整數。 當輸入 -1 時循環結束。 對於輸入的每個數字,檢查它是否非常奇數(或超級非常奇數)。
如果有人能解釋一下超級奇數部分的要求是什么,那將不勝感激,因為英語不是我的第一語言。 我不需要任何人來完成整個作業。 我只需要幫助解決我遇到的問題。提前謝謝你!
public class TestOddNumbers {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int userInput;
int sum = 0;
int product = 1;
final int EXIT = -1;
final int MIN = 101;
final int MAX = 100001;
String total = "";
do{
System.out.println("Please enter a positive whole number between "
+ "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
+ "when you are done entering numbers.");
userInput = stdin.nextInt();
total += String.valueOf(userInput);
if(userInput==EXIT){
System.out.println("Thanks for playing!");
break;
}
if(userInput > MIN && userInput < MAX){
if(userInput % 2 == 1){
System.out.println("It's an odd number");
}
else{
System.out.println("Not an odd number");
}
if(total.length() %2 == 1){
System.out.println("It also has an odd number of digits");
}
else{
System.out.println("Does not have an odd number of digits");
}
boolean[] allOdds = {true};
String.valueOf(userInput).chars().forEach(i -> {
if(Integer.parseInt(String.valueOf((char)i))%2==0){
allOdds[0] = false;
}
});
if(allOdds[0]){
System.out.println("all digits are odds");
}
else{
System.out.println("all digits are not odds");
}
// The sum code block
while (userInput != 0) {
sum += userInput % 10;
userInput /= 10;
}
if((sum += userInput) % 2 == 1){
System.out.println("The sum of the digits are odd");
}
else{
System.out.println("The sum of the digits are not odd");
}
// The product code block
while(userInput != 0) {
product *= userInput % 10;
userInput /= 10;
}
if((sum *= userInput) % 2 == 1){
System.out.println("The product of the digits are odd");
}
else{
System.out.println("The product of the digits are not odd");
}
}
else{
System.out.println("Check the bounds again!!");
}
System.out.println();
}
while(userInput > MIN || userInput < MAX);
}
}
public class Product_of_Digits_of_a_Number
{
// accepting a three digit number
static void calc(int n)
{
System.out.println("\f"); // clearing the screen
// extracting each digit from the number
int units = n % 10; // taking the units' digit out of the number and storing in u
int tens = (n / 10) % 10; //taking tens' digit and string in tens.
int hundreds = (n / 10) / 10; //taking hundreds' digit and storing in hundreds
// performing the required calculation on the digits
int a = units * tens * hundreds; // storing the product in a
// printing the digits
System.out.println("the product of digits of " + n + " is " + a);
}
}
如果您想要總和,則代碼是相同的,但是您已將總和存儲在變量a
。
public class MyClass {
static void myFun(int n)
{
int sum = 0;
int prod = 1;
while (n != 0)
{
sum = sum + n % 10;
prod = prod * (n % 10);
n = n / 10;
}
System.out.print("Product and Sum of digits of number given is:"+prod+" and " +sum);
}
public static void main(String[] a) {
myFun(25);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.