سؤال

Basically I need this while loop to perform 4 times

It will ask user for input then use that input in the nested if statements. (repeat 4 times)

The first prompt will show up and I am able to type an input.

but after that it just gives me an error.

and keeps pointing the error to "int userInput = scan.nextInt();"

I have no idea whats wrong with it. Can someone guide me? thanks

    int count = 0;

    while (count < 4 ) {

        System.out.println(prompt1);
        int userInput = scan.nextInt();

        (some nested if statements here to do my task, they all have
         count++ at the end)

    }
هل كانت مفيدة؟

المحلول

Always check if the nextInt is available before asking for it :

if(scan.hasNextInt())
{
 int userInput = scan.nextInt();
 // do something
}

نصائح أخرى

You need to define scan

Scanner scan = new Scanner(System.in);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top