简体   繁体   English

Java 循环直到用户输入值 0。值必须在 1-4 之间,如果超过 4,要求用户再次尝试输入

[英]Java loop until user enters a value of 0. The values must be between 1-4 and if over 4, ask user to try inputting again

I need to create a loop where the user can input any amount of numbers between 1-4 and then I calculate the average.我需要创建一个循环,用户可以在其中输入 1-4 之间的任意数量的数字,然后计算平均值。 Typing 0 will end the program and calculate the average.键入 0 将结束程序并计算平均值。 Any value greater than 4 or less than 0 should not count and ask the user to input the value again.任何大于 4 或小于 0 的值都不应计数并要求用户再次输入该值。 I'm stuck on the last part.我被困在最后一部分。 I'm not sure if the while loop is the correct loop to use either.我不确定 while 循环是否是正确的循环使用。 Thanks for any help谢谢你的帮助

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double sum = 0;
    double count = 0;

    while(input.hasNextInt()) {
        int num = input.nextInt();
        if (num == 0)
            break;
        if (num > 4)
            System.out.println("Invalid number");
        sum += num;
        count += 1;
    }

    System.out.println("Average: " + sum/count);
}

You will always hit the lines:你总是会碰到以下几行:

sum += num;
count += 1;

Because the code just drops through from the second if statement.因为代码只是从第二个 if 语句中退出。

The following would work - note the else if and else blocks will only be executed when the first if drops through:以下是可行的 - 请注意 else if 和 else 块只会在第一个 if 掉线时执行:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    double sum = 0;
    double count = 0;

    while(input.hasNextInt()) {
        int num = input.nextInt();

        if (num == 0) {
            break;
        }
        else if (num > 4) {
            System.out.println("Invalid number");
        }
        else {
            sum += num;
            count += 1;
        }
    }

    System.out.println("Average: " + sum/count);
}

暂无
暂无

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

相关问题 Java 循环要求输入,直到用户输入可接受的内容 - Java loop asking for input until user enters something acceptable 要求用户再次输入直到正确 - ask user to put input again until correct 我将如何在 java 中创建一个循环,要求用户从数组列表中选择一个 class 并且如果他们输入无效值再次询问? - How would I make a loop in java that will ask the user to pick a class from the array list and if they enter an invalid value to ask again? 如何在下面的代码中使用循环,以便在用户再次输入任何字母程序时要求输入数字而不是终止程序? - how can loop be used in following code so that if user enters any alphabet program again ask to enter a nuumber rahter than terminating a program? 用户在字符串中输入值后退出While循环(Java) - Exiting While Loop After User Enters Value in String (Java) 如何重复输入字符串,直到用户输入关键字以停止Java中的循环 - How to repeatedly enter a string until user enters keyword to stop loop in java 在用户在Java中输入空白行之前,我将如何进行此循环? - How would I make this loop until the user enters a blank line in java? Java 代码添加 do while 循环以使其运行直到用户输入 0 - Java code adding do while loop to make it run until user enters 0 接受 int 值直到用户输入零,然后找到最小正整数的 Java 程序 - Java program that accepts int values until the user enters a zero, and then finds the minimum positive integer 将二进制转换为int并询问用户是否要重试 - Convert binary to int and ask user if they would like to try again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM