简体   繁体   English

比较两个数字的数字并求和时发出

[英]Issue while compare and sum digits of two numbers

So, the thing is that I have two lines with two numbers on each line.所以,问题是我有两行,每行有两个数字。 On each line the numbers are separated by space.在每一行上,数字由空格分隔。 What I'm trying to do is to compare which number is bigger on the line and sum digits of the bigger number.我想做的是比较线上哪个数字更大,并对更大数字的数字求和。

Example例子

1000 2000
2000 1000

In this case the output should be在这种情况下,output 应该是

2
2

because on the first line the right numbers is bigger and the sum of it digits is 2. The second line is the opposite.因为在第一行右边的数字更大,它的数字之和是 2。第二行是相反的。

What I have so far is this到目前为止我所拥有的是这个

    int sum = 0;

    for (int i = 0; i <= 2; i++) {
        String numbers = scanner.nextLine();

        String[] parts = numbers.split(" ");
        double leftNumber = Double.parseDouble(parts[0]);
        double rightNumber = Double.parseDouble(parts[1]);

        if ( leftNumber > rightNumber ) {
            while (leftNumber > 0) {
                sum = (int) (sum + leftNumber % 10);
                leftNumber = leftNumber / 10;
            }
        } else {
            while (rightNumber > 0) {
                sum = (int) (sum + rightNumber % 10);
                rightNumber = rightNumber / 10;
            }
        }
        System.out.println(sum);
    }

Result that I've get is我得到的结果是

2
4

it is adding the second line result to the first one.它将第二行结果添加到第一行。 How can I avoid this?我怎样才能避免这种情况?

Why not make things easier on yourself and just treat the integers as strings for the purpose of summing the digits?为什么不让事情变得更简单,而只是将整数视为字符串以求和数字呢?

    while (scanner.hasNext()) {
        int larger = Math.max(scanner.nextInt(), scanner.nextInt());
        String sLarger = String.valueOf(larger);
        int digitsSum = 0;
        for (int i = 0; i < sLarger.length(); i++) {
            digitsSum += sLarger.charAt(i) - '0';
        }
        System.out.printf("Larger number %s sums to %d%n", sLarger, digitsSum);
    }

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

相关问题 任何数字的总和 - Sum of any digits of numbers 数字总和问题 - Sum of digits issue 打印字符串中的数字(不是数字)的总和 - To print the sum of the numbers(not digits) in a string 计算小于或等于N的两个数字的对数,以使对数的数字总和为质数 - Count number of pairs of two numbers less than or equal to N such that Sum of the digits of numbers of pair is Prime 找出前两位数字和后两位数字之和的平方等于数字本身的所有四位数字 - find all four digit numbers for which the square of the sum of the first two digits and the last two digits equal the number itself 如何将两个整数的数字相加? - How to sum the digits of two integers? 找到最大和最小整数,然后计算这两个数字的中间数字之和 - finds the highest and lowest integers and then calculates the sum of the middle digits of those two numbers 打印小于或等于56且数字总和大于10的两位数 JAVA - print two digit numbers which are less than or equal to 56 and the sum of digits is greater than 10 JAVA 需要从循环中对随机数中的数字求和 - Need to sum digits in random numbers from loop 递归查找包括负数在内的数字总和 - Recursively find sum of digits including negative numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM