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

时间:2014-01-30 16:17:32

标签: java

我正在尝试制作一个可以分析用户的两位数字的程序。我使用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 
.....

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

4 个答案:

答案 0 :(得分:3)

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

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

你看到了这种模式吗?

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是您的代码,但只需稍加修改即可正常工作: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;
            }
        }
    }
}

答案 3 :(得分:-1)

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

    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;