Java For循环数据验证

时间:2016-01-20 10:39:29

标签: 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

4 个答案:

答案 0 :(得分:3)

只需在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--;
}

答案 1 :(得分:0)

如果您将for循环更改为此

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();

    while(preference != 1 && preference != 2 && preference != 3) {
        System.out.println ("Sorry that is not a valid option");
        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) {
        nothing ++;
    } else if(preference == 2) {
        sweetner ++;
    } else if(preference == 3) {
        sugar ++;
    }
}

这将解决它

答案 2 :(得分:0)

将if / else替换为Switch / Case:

preference = input.nextInt();
switch(preference) {
  case 1: 
    nothing++;
    break;
  case 2:
    sweetner++;
    break;
  case 3:
    sugar++;
    break;
  default:
    System.out.println("Sorry, thats not a valid option! Please pick a valid option");
    preference = input.nextInt();
    person--;
    break;

}

答案 3 :(得分:0)

我做了while循环并且它有效,非常感谢Dan。虽然循环是书中的下一个部分,但我正在努力,所以它给了我一个良好的开端。非常感谢你和其他所有回复的人,其他一些建议对我来说似乎有些先进,但我知道如果我将来需要它们,我总能参考这些意见。再次感谢,罗宾。