Java虽然True语句应该在它应该已经破坏时重复

时间:2017-08-31 15:58:10

标签: java

所以在我的代码中我试图在一个循环中添加数字,我的意思是,一旦你输入一个数字,它将它添加到前一个并给你新的总和。我有这个工作正常,但当我输入一个0,它结束程序,给你一个消息说不管最后一笔金额,它重新启动程序,我似乎无法使它结束。这是我用来开展工作的网站from。我正在练习26.

这是我的代码:

package July28Restart;

import java.util.Scanner;

public class Ex26SumOfManyNumbers {
    public static void main(String args[]){
        Scanner reader = new Scanner(System.in);

        int sum = 0;
        while (true){

            System.out.println("Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last.");
            int read = Integer.parseInt(reader.nextLine());

            if (read == 0){
                break;
            } else if (read != 0){
                while (true){
                    sum = sum + read;
                    System.out.println("Sum now: " +sum);
                    read = Integer.parseInt(reader.nextLine());
                    if (read == 0) {
                        System.out.println("The sum in the end was: "+sum);
                        break;              
                    }   
                }   
            }
        }
    }
}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

因为如果你在输入另一个数字后输入0,你就会从第二个循环中断到第一个循环,并且只有你输入0两次(直接在另一个之后)才会退出

更正后的代码:

package July28Restart;
import java.util.Scanner;

public class Ex26SumOfManyNumbers {
    public static void main(String args[]) {
        Scanner reader = new Scanner(System.in);

        int sum = 0;
        while (true) {

            System.out.println(
                    "Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last.");
            int read = Integer.parseInt(reader.nextLine());

            if (read == 0) {
                exitProgram(sum);
            } else if (read != 0) {
                while (true) {
                    sum = sum + read;
                    System.out.println("Sum now: " + sum);
                    read = Integer.parseInt(reader.nextLine());
                    if (read == 0) {
                        exitProgram(sum);
                    }

                }

            }

        }

    }

    private static void exitProgram(int sum) {
        System.out.println("The sum in the end was: " + sum);
        System.exit(0);

    }

}

答案 1 :(得分:2)

你可以使用标签来打破内循环中的外循环:

outer:
while (true){
    // ...
    while (true){
        // ...
        if (read == 0) {
            System.out.println("The sum in the end was: "+sum);
            break outer;              
        }   
    }   
    // ...
}

答案 2 :(得分:2)

抱歉,刚看到你的问题。正如Jaroslaw Pawlak所解释的那样,你可以在这里使用外部,或者你可以只重构并使用一个。看起来像是:

public class Ex26SumOfManyNumbers {
    public static void main(String args[]){
        Scanner reader = new Scanner(System.in);
        calSum(reader);

    }

    public static void calSum(Scanner reader) {
        System.out.println("Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last.");
        int sum = 0;
        while (true){
             int read = Integer.parseInt(reader.nextLine());
             if(read != 0) {
                 sum = sum + read;
                 System.out.println("Sum now: " +sum);
             } else {
                 break;
             }
        }
        System.out.println("The sum in the end was: " + sum);
    }
}