문제

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