繁体   English   中英

分析两位数字并打印其各个数字的总和

[英]Analyze a two digit number and print sum of its individual digits

我正在尝试制作一个程序,可以分析用户的两位数字。 我使用Scanner对象来获取输入,然后使用数组组合。 这是我的代码:

class Calculate {
    public static void calculate() {
        Scanner br = new Scanner(System.in);

        int[] c = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int[] d = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

        int sum;
        int a = br.nextInt();
        System.out.println("Digit is:" + a);

        for (int i = 0; i < 10; i++) {
            int e = 0;
            if (a == ((c[i] * 10) + d[e])) {
                sum = ((c[i] * 10) + d[e]);
                System.out.println("First digit is:" + c[i] 
                        + " Second digit is:" + d[e] 
                        + "\n" + "Sum of digits=" + sum);
            } else if (i == 9) {
                i = 0;
                e++;
            } else if (e == 10) {
                System.out.println("INVALID NO. HAS BEEN DETECTED");
            }
        }
    }
}

我的预期输出(例如,如果用户输入56):

Digit is:56 
First Digit:5 Second digit:6
Sum of digits=11

但是,输出没有正确输出; 这是无限的输入。喜欢这个:

56
Digit is:56
23
89 
45 
.....

就这样,它陷入了一个循环。 我怎么能纠正这个?

如果你想得到一个数字的第一个和第二个数字,而不是那个疯狂的for循环,请使用这种技术。

int number = 457;
int hundredsDigit = number % 1000 - number % 100;
int tensDigit= number % 100 - number % 10
int onesDigit = number % 10 - number % 1;

你看到了这种模式吗?

Scanner br = new Scanner(System.in);
int[] c = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int[] d = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum;
int a = br.nextInt();
System.out.println("Digit is:" + a);
if (a < 0 || a > 99)
    System.out.println("INVALID NO. HAS BEEN DETECTED");
else
    System.out.println("First digit is:" + a / 10 + " " + "Second digit is:" + a % 10 + "\n" + "Sum of digits=" + (a / 10 + a % 10));

这是你的代码,但几乎没有修改,以便它工作正常:p

class Calculate {
    public static void calculate() {
        Scanner br = new Scanner(System.in);

        int[] c = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int[] d = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

        int sum;
        int a = br.nextInt();
        System.out.println("Digit is:" + a);

        int e = 0;
        for (int i = 0; i < 10; i++) {

            if (a == ((c[i] * 10) + d[e])) {
                sum = ((c[i] * 10) + d[e]);
                System.out.println("First digit is:" + c[i] 
                        + " Second digit is:" + d[e] 
                        + "\n" + "Sum of digits=" + sum);
                break;
            } else if (i == 9) {
                i = 0;
                e++;
            } else if (e == 10) {
                System.out.println("INVALID NO. HAS BEEN DETECTED");
                break;
            }
        }
    }
}

这是另一种方式(只是为了显示另一种可能性):

    String number = String.valueOf(a);

    String firstNumber =  number.substring(0, 1);
    String secondNumber = number.substring(1);

    sum = Integer.parseInt(firstNumber) +  Integer.parseInt(secondNumber);

并记住初始化总和不仅声明它:

    int sum = 0; 

还记得在使用完毕后关闭扫描仪br

    br.close;

暂无
暂无

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

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