2

我将如何打印一条消息说“错误:您必须输入一个介于 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));
4

8 回答 8

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*/ }
}
于 2013-10-28T13:25:00.447 回答
2

你可以这样做。我使用正则表达式来防止 NumberFormatExcption。

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));
于 2013-10-28T13:31:10.207 回答
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
于 2013-10-28T13:27:34.860 回答
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大多数答案的解决方案提供的重新计算。


此答案中的代码未经测试

于 2013-10-28T13:31:49.107 回答
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");
}
于 2013-10-28T13:23:27.673 回答
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));
于 2013-10-28T13:28:01.387 回答
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);
于 2013-10-28T13:26:14.950 回答
-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));
}
于 2013-10-28T13:27:16.800 回答