元のコードを最小限に変更する最も簡単な方法は次のとおりです。
int number;
do {
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
if((number < 0) || (number > 5) {
//show error message
continue; //continue isn't absolutely necessary here, but perhaps for readability
}
} while (!(number >= 0 && number <= 5));
これは少し不格好で冗長だと思いますが、基本的に同じ条件を2回チェックしています。次のような方法を使用します。
int number;
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
while(true) {
number = Integer.parseInt(textinput);
if((number >= 0 && number <= 5)) {
//show error message and prompt for another input
contine; //As with before, continue isn't necessary here, but could add readability
} else /*input was good*/ { break; /*exit while loop*/ }
}