显示来自while循环的用户输入

时间:2017-09-12 20:29:20

标签: java while-loop

我需要编写一个程序,提示用户输入标记值为-1的正整数。程序需要在一行上显示输入的整数(不包括-1),然后显示输入的整数之和。我做了一些搜索,看起来我需要创建一个列表来存储这些数字?我还很新,并且真的不知道该怎么做。我试过谷歌搜索,但它让我感到困惑。到目前为止,这是我的代码:

import java.util.Scanner;

public class InputSum {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter positive integers. Enter -1 to stop. ");
        int number = scan.nextInt();
        int sum = 0;
        while (number != -1) {
            if (number < -1) {
                System.out.println("Invalid input. Try again.");
            } else
                sum += number;
            number = scan.nextInt();
        }
        System.out.println("The sum: " + sum + ".");
    }
}

顺便说一下,如果我做错了,这是我第一次提交对不起!

2 个答案:

答案 0 :(得分:0)

您可以使用do-while语句以这种方式执行此操作:

import java.util.Scanner;

public class InputSum {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter positive integers. Enter -1 to stop. ");
        int number = 0;
        int sum = 0;
        StringBuilder sb = new StringBuilder();
        do{
            try {
                number = scan.nextInt();

                if(number < -1){
                    System.out.println("Invalid input. Try again.");
                }else if(number > -1){
                    sum += number;
                    sb.append(number + " ");
                }

            } catch (InputMismatchException e) { // handle invalid integers
                System.out.println("Invalid input. Try again.");
            }
        }while (number != -1);

        System.out.println("Numbers entered: " + sb.toString().trim() + ".");
        System.out.println("The sum: " + sum + ".");
    }
}

这将迭代直到用户输入-1。最后,它将显示正整数和输入数字的总和。我正在使用StringBuilder来累积数字。您也可以使用列表,但我觉得这样更容易。

修改

这是另一种没有try-catch块的方法:

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter positive integers. Enter -1 to stop. ");
        int number = 0;
        int sum = 0;
        StringBuilder sb = new StringBuilder();
        do{
            if(scan.hasNextInt()){
                number = scan.nextInt();

                if(number < -1){
                    System.out.println("Invalid input. Try again.");
                }else if(number > -1){
                    sum += number;
                    sb.append(number + ", ");
                }
            }else{
                System.out.println("Invalid input. Try again.");
                scan.next(); // reset the scanner
            }
        }while (number != -1);

        System.out.println("Numbers entered: " + sb.delete(sb.length() - 2, sb.length()) + ".");
        System.out.println("The sum: " + sum + ".");
}

正如本评论中所提到的,我正在使用hasNextInt来验证输入是否是一个有效的整数,然后才能读取它。这是为了防止在用户输入无效整数时引发的异常。我也用逗号分隔了数字。

答案 1 :(得分:0)

您可以使用StringBuilder存储数字

import java.util.Scanner;

public class InputSum {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter positive integers. Enter -1 to stop. ");
    int number = scan.nextInt();
    int sum = 0;
    StringBuilder sb = new StringBuilder();
    while (number != -1) {
        if (number < -1) {
            System.out.println("Invalid input. Try again.");
        } else
            sum += number;
        number = scan.nextInt();
        sb.append(number + " +  ");
    }
    System.out.println("The sum: " + sum + ".");
}

}