【问题标题】:How to find the product and sum of digits of a number in java?如何在java中找到数字的乘积和数字总和?
【发布时间】:2017-01-29 02:50:28
【问题描述】:

我需要找到输入数字的数字的总和和乘积。我把总和部分记下来了,但唯一的问题是,当我第一次输入一个数字时,它给了我正确的答案,但是当我输入另一个数字时,它只是将第一个数字的总和加上第二个数字在一起,然后检查两个和的总和是否为奇数(抱歉,如果它有点混乱)。对于代码的产品部分,我似乎不知道该怎么做。

最后,如果总和和产品都是奇数,我需要它说输入的数字是一个极奇数。

这是作业:

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

   }
}

【问题讨论】:

  • 您在 sum 循环中操纵输入值的值,那么您为什么希望您可以再次这样做呢?为什么不直接在“求和循环”中创建产品?
  • @Tom 你的意思是 userInput /=10;线?如果我没有那条线,它将无法正常运行,我将如何更改它?以及如何在 sum 循环中创建产品?
  • 您会如何处理需要更改但又想保持其原始状态以备后用的“某些东西”?你会创建一个副本吗? “我将如何在 sum 循环中创建产品?” 检查两个 while 循环体......它们有什么区别?你能在第一个循环中做与第二个循环不同的“事情”吗?那么结合两个循环体?
  • @Tom 所以我想出了如何找到该产品,但我仍然无法弄清楚如何保持其原始价值。我知道我可能遗漏了一些非常明显的东西,但我就是无法理解。
  • “所以我想出了如何找到产品”这是你的主要问题,所以现在不是一切都好吗?保留原始值只是解决问题的另一种方法。

标签: java


【解决方案1】:
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中。

【讨论】:

  • 那个方法真的很简单! :)
猜你喜欢
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2014-10-16
  • 1970-01-01
相关资源
最近更新 更多