Java For 循环数据验证

标签 java validation loops if-statement for-loop

我目前正在大学里做一些 Java 练习,我已经在这个练习上停留了大约 5 个小时了!我正在练习 For 循环并让循环询问 5 次 1 到 3 的数字。测试时,如果我输入无效选择,它会继续并将无效选择包含为零,当输入了无效输入,但它仍然会继续,直到循环结束,我知道有一种方法可以返回到选择的开始,但我无法弄清楚。 我到处寻找解决方案,但找不到!我知道这不会太多,而且我已经几天没有回到大学了,所以我不能问讲师,我真的很想继续学习下一章。

这是我的代码(我知道它可能有点乱!!),谢谢,Rob

   import java.util.Scanner;

/* this is s a survey of how 5 people sweeten thier coffee */

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

        int person, preference, nothing, sugar, sweetner;

        String pluralone = "People dont";
        String pluraltwo = "People use";
        String pluralthree = "People use";

        person = 0;
        preference = 0;
        nothing = 0;
        sugar = 0;
        sweetner = 0;

        for (person = 1; person <= 5; person++)

        {
            System.out.println("How do you sweeten your coffee");
            System.out.println("1. I Don't");
            System.out.println("2. With Sweetner");
            System.out.println("3. With Sugar");

            preference = input.nextInt();

            if (preference != 1 && preference != 2 && preference != 3)
                System.out.println("Sorry that is not a valid option");

            else if (preference == 1)
                nothing++;

            else if (preference == 1)
                sweetner++;

            else
                sugar++;
        }

        System.out.println("Survey Report");

        System.out.println("#############");

        if (nothing < 2)

        {
            pluralone = "person doesnt";
        }

        System.out.println(nothing + "  " + " " + pluralone + " sweeten thier coffee");

        if (sweetner < 2)

        {
            pluraltwo = "person uses";
        }

        System.out.println(sweetner + "  " + pluraltwo + " " + "sweetner to sweeten thier coffee");

        if (sugar < 2)

        {
            pluralthree = "person uses";
        }

        System.out.println(sugar + "  " + pluralthree + " " + "sugar to sweeten thier coffee ");

    }
}

enter image description here

最佳答案

只需在一个 while 循环中要求用户选择,这样它就不会继续,直到输入了一个有效的选项,比如:

preference = input.nextInt();
while (preference != 1 && preference != 2 && preference != 3) {
    System.out.println("Sorry that is not a valid option");
    preference = input.nextInt();
}

或者,您可以减少 if 语句中的 person 以引起 for 循环的另一次迭代,但这有点 hacky:

if (preference != 1 && preference != 2 && preference != 3) {
    System.out.println("Sorry that is not a valid option");
    person--;
}

关于Java For 循环数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897710/

相关文章:

java - 响应客户端后如何提取有效负载?

java - 从jar中读取资源文件

php - Javascript/PHP 中的 HTML 验证器? (不一定是 XML)

asp.net-mvc - ASP.NET MVC : How to validate an Ajax form with a specified UpdateTargetID?

c - 使用 isalpha() 导致文本转储

php - 从数组中的每个字符串中提取一个或多个符合条件的子字符串

java - JBoss 类加载不兼容的类更改错误

java - 给定这段代码我将如何排序

asp.net-mvc - 如何从不在数据库表中的模型创建验证字段?使用 MVC4

c - 再次打印对函数的最后一次调用