繁体   English   中英

Java - 如何继续询问用户输入?

[英]Java - How do I continue to ask the user for input?

我已经在这个程序中呆了几个小时,但我仍然不明白其中的逻辑。 该程序要求用户输入整数。

一旦用户输入了整数,它将向用户输出所有输入的正数、负数和总数,以及这些数字的平均值。

这是我被困的地方:

然后它会询问用户是否要继续。 用户将输入'y''n'以表明他们是否希望继续。 如果用户输入'y',循环将再次运行,如果他们输入'n',它将显示一个再见消息。 我不知道如何设置。

我已经尝试了一个 do while 循环,目前我正在为程序使用一个 while 循环。 我还缺少一个语句,让用户知道如果没有输入其他数字,他们只输入零。 我也不知道如何设置,但我尝试了一个 if 语句,但它不起作用..我的代码如下。 我正在使用 Java 8。

公共类 CountPositiveNegativeAndAverageOfIntsProgram {

public static void main(String [] args)
{

     System.out.print("Enter an integer, the input ends if it is 0: ");
     //Creating a scanner object
    Scanner input = new Scanner(System.in);
    int userInput = input.nextInt();

    //creating variables that will be used in the program.
    int countPositive = 0;
    int countNegative = 0;
    int total = 0;
    double average = 0;

    while(userInput != 0)
    {            

        if(userInput > 0)
        {
        //using a counter to add how many positive numbers are entered.
        countPositive++;
        }
        else
        {
          countNegative++;      
        }

        total += userInput;
        //I added 0.0 to avoid integer division. This felt like a small hack.
        average = total / (countPositive + countNegative + 0.0);
        userInput = input.nextInt();

    }      

     System.out.println("The number of positives is: " + countPositive);
     System.out.println("The number of negatives is: " + countNegative);
     System.out.println("The total is: " + total);
     System.out.println("The average is : " + average);

     System.out.print("Continue? (y/n) ");
     String s = input.next();




}

}

一个解决方案,虽然不是很好,只是创建一个函数,当用户提供y作为响应时,您可以在其中递归调用。

    public static void main(String [] args) {
        request();
    }

    private static void request() {
        System.out.println("Enter an integer, the input ends if it is 0: ");
        //Creating a scanner object
        Scanner input = new Scanner(System.in);
        int userInput = input.nextInt();

        //creating variables that will be used in the program.
        int countPositive = 0;
        int countNegative = 0;
        int total = 0;
        double average = 0;

        while(userInput != 0)
        {

            if(userInput > 0)
            {
                //using a counter to add how many positive numbers are entered.
                countPositive++;
            }
            else
            {
                countNegative++;
            }

            total += userInput;
            //I added 0.0 to avoid integer division. This felt like a small hack.
            average = total / (countPositive + countNegative + 0.0);
            userInput = input.nextInt();

        }

        System.out.println("The number of positives is: " + countPositive);
        System.out.println("The number of negatives is: " + countNegative);
        System.out.println("The total is: " + total);
        System.out.println("The average is : " + average);

        System.out.println("Continue? (y/n) ");

        String response = input.next();

        if (response.equals("y")) {
            request();
        } else {
            System.out.println("Goodbye!");
        }
    }

Java 8 解决方案如果你真的想稍微改进一下,你可以做一些事情。

  • 使用 Scanner#hasNextInt 来确定扫描器是否有 Int 要使用。
  • 维护一个代表输入的整数对象列表,而不是使用多个变量来跟踪负数、正数和总数。
  • 使用 Stream#filter 检查正值和负值,然后使用 count() 告诉我们 Stream 中与此过滤器匹配的元素数。
  • 分别使用 IntStream#sum 和 IntStream#average 确定整数流的总和和平均值。
    private static void request(Scanner scanner) {
        System.out.println("Enter an integer, the input ends if it is 0: ");

        List<Integer> values = new ArrayList<>();

        while(scanner.hasNextInt()) {
            int value = scanner.nextInt();

            if (value == 0) {
                break;
            }
            values.add(value);
        }
        System.out.println("The number of positives is: " + values.stream().filter(value -> value > 0).count());
        System.out.println("The number of negatives is: " + values.stream().filter(value -> value < 0).count());
        System.out.println("The total is: " + values.stream().mapToInt(value -> value).sum());
        System.out.println("The average is : " + values.stream().mapToInt(value -> value).average().orElse(0));
        System.out.println("Continue? (y/n) ");

        String response = scanner.next();

        if (response.equals("y")) {
            request(scanner);
        } else {
            System.out.println("Goodbye!");
        }
    }

暂无
暂无

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

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