0

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

''''

4
  • 1
    Without seeing any code it's really not possible to tell you what is wrong with your code. Commented Mar 22, 2022 at 14:02
  • Yeah sorry I'm new to this just edited on the code now. Commented Mar 22, 2022 at 14:11
  • 1
    You are not actually looping. 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.html Commented Mar 22, 2022 at 14:16
  • @maloomeister is correct; but once you fix that and put the code you want to loop into a block, it still won't loop because the loop condition is currently while(!correctChoice) which evaluates to false. Commented Mar 22, 2022 at 14:19

1 Answer 1

1

Your 2 main problems are:

First, your loop does not contain any actual operations except a noop

while(!correctChoice);

is equal to

while(!correctChoice) {
    ;
}

And your second logical error is that you initialize the value of correctChoice with true, so that your loop condition is never fullfilled and the loop never entered.

You should initialize that value as false, and then inside the loop change it to true once the correct choice has been made to exit the loop.

A corrected version of your code would look something like this:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n;
    boolean correctChoice = false;

    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!");
        } else if (n > 3) {
            System.out.println("Functionality currently not availible. Please try again");
        } else {
            System.out.println("Value OK");
            correctChoice = true;
        }
    }
    System.exit(0);
}

There are more things that could be improved, like catching a InputMismatchException to handle your user not entering a number but normal text, but I think this should be enough to get you started.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.