简体   繁体   中英

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. 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 aparticipation; that is declaring a variable. To assign a value to the variable after that you just do aparticipation = keyboard.nextInt();

You already declared it so you dont have to tell the compiler that its an int again.

The error is occurring because you are trying to declare pparticipation and aparticipation again within the loop. Remove the type (int) from in front of those two variables.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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