简体   繁体   English

使while循环重新询问用户的输入

[英]Make while loop re-ask for input from user

So in this section of my program I'm trying to make the program re-ask for input from the user. 因此,在我的程序的这一部分中,我试图让程序重新询问用户的输入。

The problem is that is says the int have already been declared. 问题是说已经声明了int。 But how do I get the input for the question again? 但是如何再次获得问题的输入?

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter possible and actual points for participation: ");
int pparticipation = keyboard.nextInt();
int aparticipation = keyboard.nextInt();

while (aparticipation > pparticipation || pparticipation < 0){
   System.out.println("Please enter possible and actual points for participation: ");
   int pparticipation = keyboard.nextInt();
   int aparticipation = keyboard.nextInt();
}

You declared the variables twice. 您将变量声明了两次。 Removing the "int" from the variables in the loop should get it working. 从循环中的变量中删除“int”应该可以使它工作。

int aparticipation; that is declaring a variable. 那是声明一个变量。 To assign a value to the variable after that you just do aparticipation = keyboard.nextInt(); 要在此之后为变量赋值,只需执行aparticipation = keyboard.nextInt();

You already declared it so you dont have to tell the compiler that its an int again. 你已经声明了它,所以你不必再告诉编译器它是一个int。

The error is occurring because you are trying to declare pparticipation and aparticipation again within the loop. 发生错误是因为您试图在循环内再次声明pparticipation和aparticipation。 Remove the type (int) from in front of those two variables. 从这两个变量前面删除类型(int)。

All you have to do is to change the following :- 您所要做的就是更改以下内容: -

while (aparticipation > pparticipation || pparticipation < 0){
   System.out.println("Please enter possible and actual points for participation: ");
   int pparticipation = keyboard.nextInt();
   int aparticipation = keyboard.nextInt();
}

to

while (aparticipation > pparticipation || pparticipation < 0){
   System.out.println("Please enter possible and actual points for participation: ");
   pparticipation = keyboard.nextInt();
   aparticipation = keyboard.nextInt();
}

The variables are already declared, so don't do it again. 变量已经声明,所以不要再这样做了。

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

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