[英]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.