[英]Java For Loop data validation
I am currently doing some java excersises in uni and ive been stuck on this one for about 5 hours now! 我目前正在大学做一些java练习,现在我已经被困在这个大约5个小时了! I am practising For loops and have the loop ask 5 times for a number from 1 to 3. When testing, if I enter an invalid selection it carries on and includes the invalid selection as a zero, I have got an error message working when an invalid input is entered but it still carries on until the loop finishes, I know there is a way to return to the beggining of the selection but I cant figure it out.
我正在练习For循环并让循环询问5次从1到3的数字。测试时,如果我输入无效选择它继续并包括无效选择为零,我有一个错误消息工作时,输入了无效的输入,但它仍然继续,直到循环结束,我知道有一种方法可以返回到选择的开始,但我无法弄明白。 I have searched everywhere for a solution but cannot find it!
我到处寻找解决方案但找不到它! I know it cant be much and I'm not back in uni for a few days so I cant ask the lecturer and I would really like to crack on to the next chapter.
我知道它不会太多,而且我几天都没有回到大学,所以我不能问讲师,我真的想继续下一章。
Here is my code (I know its probably a bit scrappy!!), thanks, Rob 这是我的代码(我知道它可能有点杂乱!!),谢谢,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 ");
}
}
just ask for the users selection in a while loop so that it doesn't continue until a valid option has been entered, something like: 只需要在while循环中询问用户选择,以便在输入有效选项之前它不会继续,例如:
preference = input.nextInt();
while (preference != 1 && preference != 2 && preference != 3) {
System.out.println("Sorry that is not a valid option");
preference = input.nextInt();
}
alternatively you could decrement person
in your if statement to cause another iteration of the for loop, but that's a bit hacky: 或者你可以减少if语句中的
person
来导致for循环的另一次迭代,但这有点hacky:
if (preference != 1 && preference != 2 && preference != 3) {
System.out.println("Sorry that is not a valid option");
person--;
}
If you change your for loop to this 如果您将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 ++;
}
}
This will fix it 这将解决它
Replace the if/else with an Switch/Case: 用switch / Case替换if / else:
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;
}
I did the while loop and it works, thank you very much Dan. 我做了while循环并且它有效,非常感谢Dan。 While loops are the next part in the book that im moving onto so its given me a head start too.
虽然循环是书中的下一个部分,但我正在努力,所以它给了我一个良好的开端。 Many thanks to you and everyone else who replied, a couple of the other things suggested seem a little advanced for me but I know I can always refer to the comments if I need them in the future.
非常感谢你和其他所有回复的人,其他一些建议对我来说似乎有些先进,但我知道如果我将来需要它们,我总能参考这些意见。 Thanks again, Robin.
再次感谢,罗宾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.