I am wanting to create a simple programme that will terminate when either the number 2 or 3 is guessed and whenever a number is not in that range I want an error message to pop up and say try again and then I want the user to be able to type in another number until it they are correct.
I have tried using if statements with my 'n' either being greater than or smaller than but the programme is still not working. I let the while loop condition be true and I expected this to repeat the programme so I could try again but no such luck. ''''
package meganImpasseProject;
import java.util.Scanner;
public class looping {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
boolean correctChoice = true;
while(!correctChoice);
System.out.println("How many colours do you want?");
n = input.nextInt();
if(n<2) {
System.out.println("Invalid value, Please try again!");
n = input.nextInt();
}else if(n>3) {
System.out.println("Functionality currently not availible. Please try again");
n = input.nextInt();
}else {
System.out.println("Value OK");
}
}
System.exit(0);
''''
while(!correctChoice);<- notice the;at the end there, you have no loop body. Check out the tutorial regarding while loops: docs.oracle.com/javase/tutorial/java/nutsandbolts/while.htmlwhile(!correctChoice)which evaluates to false.