Validate Scanner input on a numeric range

前端 未结 2 931
故里飘歌
故里飘歌 2021-01-16 12:55

I\'m currently creating my first game which is executed in a console.

I\'ve been asked to validate an input which can be done with a simple code. The goal is to inpu

2条回答
  •  没有蜡笔的小新
    2021-01-16 13:27

    Because you instantiate input to be 0, but never give the user an opportunity to change this, the conditions for the first two conditionals are always false (nothing is read from the Scanner and 0 is not between min and max). Therefore, the program falls through to the else every time. Just add a statement before the do-while that will obtain a value for input from the user.

    input = scanner.nextInt();
    
    // your do-while loop
    

    (You'll also probably have to adjust the code slightly to get the type of interaction you're looking for. Hint - you're reading two values from the user.)

提交回复
热议问题