如何打印消息“错误:您必须输入0到5之间的数字”,然后允许用户再次输入
int number;
do
{
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
} while (!(number >= 0 && number <= 5));
答案 0 :(得分:3)
最简单的改变原始代码的方法如下:
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));
虽然我发现这有点笨重和多余,但你实际上是两次检查相同的情况。我会采用更像下面的方法:
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*/ }
}
答案 1 :(得分:2)
int number = -1;
do
{
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
if( textinput.match("0*[0-5]") {
number = Integer.parseInt(textinput);
}
else {
System.out.println("Error");
}
} while (!(number >= 0 && number <= 5));
答案 2 :(得分:1)
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
int number = Integer.parseInt(textinput);
while (!(number >= 0 && number <= 5)) {
textinput = JOptionPane.showInputDialog("Your number must be between 0 and 5!");
number = Integer.parseInt(textinput);
}
// do stuff
答案 3 :(得分:1)
我相信这是最不冗余和最易读的方式:
int number;
while ((number = requestNumber()) < 0 || number > 5) {
// Show your prompt
}
在某处声明此方法:
private int requestNumber() {
try {
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
return Integer.parseInt(textinput);
} catch(NumberFormatException nfe) {
return -1;
}
}
这是另一种选择:
boolean valid;
int number;
do {
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
if (number >= 0 && number <= 5) {
valid = true;
} else {
// Show your message here
valid = false;
}
} while (!valid);
这将避免重新计算大多数答案的解决方案所提供的number >= 0 && number <= 5
。
此答案中的代码未经过测试
答案 4 :(得分:0)
int number;
if(nummber>0 && number<5)
{
do
{
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
} while (!(number >= 0 && number <= 5));
}
else
{
System.out.println("you have to enter a number between 0 to 5");
}
答案 5 :(得分:0)
int number;
Boolean bolContinue = true;
do
{
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
if (!(number >= 0 && number <= 5)) {
//Log here
bolContinue = false;
}
} while (bolContinue);
答案 6 :(得分:0)
应该那么简单。
int number;
do
{
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
if(!(number >= 0 && number <= 5))
{
System.out.println("Error: you have to enter a number between 0 and 5");
}
} while (!(number >= 0 && number <= 5));
答案 7 :(得分:-1)
像这样:
int number;
do
{
String textinput = JOptionPane.showInputDialog("give me a number between 0 and 5");
number = Integer.parseInt(textinput);
if ( number > 5 || number < 0)
{
System.out.println("Please enter number between 0 to 5");
return;
}
} while (!(number >= 0 && number <= 5));
}