简体   繁体   English

如何求java中一个数的乘积和位数和?

[英]How to find the product and sum of digits of a number in java?

I need to find the sum and the product of the digits of the number entered.我需要找到输入数字的数字的总和和乘积。 I have the sum part down but the only problem with it is that when I enter a number for the first time it gives me the right answer but when I enter another number, it just adds the sum of the first number and the sum of the second number together and then checks if the sum of the two sums is odd(sorry if its kind of confusing).我有求和部分,但唯一的问题是,当我第一次输入一个数字时,它给出了正确的答案,但是当我输入另一个数字时,它只是将第一个数字的总和与第二个数字的总和相加第二个数字在一起,然后检查两个总和的总和是否为奇数(抱歉,如果它有点混乱)。 For the product part of the code I just can't seem to figure out how to do it.对于代码的产品部分,我似乎无法弄清楚该怎么做。

Finally if the sum and the product both are odd I need it to say that the number entered is an Extremely odd number.最后,如果总和和乘积都是奇数,我需要说明输入的数字是一个极奇数。

Here is the assignment:这是作业:

This Java application checks whole numbers in the interval [101, 100001] to see if they are "extremely odd" and "super extremely odd" numbers.这个 Java 应用程序检查区间 [101, 100001] 中的整数,看它们是否是“奇数”和“超级奇数”。

A whole number is "extremely odd" if...一个整数是“非常奇数”如果...

(0) it's an odd number (1) it has an odd number of digits (2) all of its digits are odd numbers (3) the sum of its digits is an odd number (4) the product of its digits is an odd number (0) 它是一个奇数 (1) 它有奇数个数字 (2) 它的所有数字都是奇数 (3) 它的数字之和是一个奇数 (4) 它的数字的乘积是一个奇数数字

A "super extremely odd" number is an extremely odd number such that... “超级奇数”是一个非常奇数,这样......

(5) all digits comprising the sum of its digits are odd numbers (6) all digits comprising the product of its digits are odd numbers (5) 所有组成它的数位和的数位都是奇数 (6) 所有组成它的数位乘积的数位都是奇数

Loop prompting the user to enter positive whole numbers.循环提示用户输入正整数。 Looping ends when a -1 is entered.当输入 -1 时循环结束。 For each number entered, check to see if its extremely odd (or super extremely odd).对于输入的每个数字,检查它是否非常奇数(或超级非常奇数)。

If somebody could just explain what the requirements for the super extremely odd numbers part are saying that would be appreciated because English is not my first language.如果有人能解释一下超级奇数部分的要求是什么,那将不胜感激,因为英语不是我的第一语言。 I don't need anyone to do the whole assignment.我不需要任何人来完成整个作业。 I just need help on the parts that I'm stuck in. Thank you in advance!我只需要帮助解决我遇到的问题。提前谢谢你!

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);
   }
}

Note 注意

If you want the sum, the code is the same but you have store the sum in variable 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);
    }
}

Source 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM