【问题标题】:JOptionPane.showInputDialog loop (using do while loop)JOptionPane.showInputDialog 循环(使用 do while 循环)
【发布时间】:2016-03-27 10:47:19
【问题描述】:

我正在尝试向用户询问 4 到 10 之间的整数。如果他们回答超出该范围,它将进入一个循环。当用户第一次正确输入数字时,它不会中断并继续执行else语句。如果用户在 else 语句中正确输入了数字,它将正确中断。

如果第一次输入正确,我怎样才能让它中断?

**我是java的初学者,如果我在这里遗漏了一些愚蠢的东西,请道歉。

public int companySize() {  

    int ansCompany;

    do {
        ansCompany = Integer.parseInt(JOptionPane.showInputDialog
                ("Please input the company size"));

        if ( ansCompany <= 4 && ansCompany <= 10 ) {
                         break;

        } else {
       ansCompany = Integer.parseInt(JOptionPane.showInputDialog
                ("Please enter a valid company size"));

        if ( ansCompany <= 4 && ansCompany <= 10 ) {
                          break;
        } // ends nested if condition
    } //ends else 
          }//ends do
    while ( ansCompany < 4 || ansCompany > 10);
    return ansCompany;
}// ends public int companySize()

我从 main 中调用它如下:

public static void main(String[] args) {

UserInput getResult = new UserInput();
int company_size =  getResult.companySize();

}// ends main

【问题讨论】:

    标签: java loops while-loop do-while


    【解决方案1】:

    while ( ansCompany &lt; 4 || ansCompany &gt; 10); 小于 3 OR 大于 11

    你想要while ( ansCompany &gt;= 4 &amp;&amp; ansCompany &lt;= 10);

    注意:你想要这个选择^^^^的原因是因为如果输入大于>或等于=使得>= than 表示 4 及以上。同样对于小于 或等于 = 使得 比意味着小于 10

    || 表示。这意味着输入必须大于 4 或小于 10。如果答案是 11,则它通过第一个条件,因此通过 if 语句。与 3 类似,它通过了小于 10 的条件。

    && 表示AND,因此必须通过第一个条件和第二个条件。

    你的if陈述在这个意义上也是错误的;

    if ( ansCompany &lt;= 4 &amp;&amp; ansCompany &lt;= 10 ) {

    应该是

    if ( ansCompany &gt;= 4 &amp;&amp; ansCompany &lt;= 10 ) {

    【讨论】:

    • 如果我希望用户输入一个介于 4 和 10 之间的数字,那么不会 while (ansCompany
    • 对不起,我知道我现在的 while 和 if 语句哪里出了问题。谢谢
    【解决方案2】:

    如果用户第一次写入错误的值,我不确定为什么需要两个相同的对话框,因为最终只会返回一个值(ansCompany)。

    通过将 do-while 语句设置为中断条件(小于 4 或大于 10),它将循环直到用户输入正确的数字。

    public int companySize() {
     int ansCompany;
    
     do {
       ansCompany = Integer.parseInt(JOptionPane.showInputDialog
          ("Please input the company size"));
    
     } while (ansCompany < 4 || ansCompany > 10);
    
     return ansCompany;
    

    }

    【讨论】:

    • 我知道那是多么多余。我不确定为什么我认为我需要 break 语句.. 但我现在当然可以看到为什么它会运行只是 do。谢谢
    • 我看到第二个对话框只有在我想告诉用户输入无效但不是必需的时候才有用。
    • 没问题,在使用代码时要小心,不要处理如果一个人写一个非整数值会出现的异常。有了这个实现,程序就会崩溃!
    • 还没有真正接触过任何捕获异常的东西,但我迫不及待地想回去实现它。
    【解决方案3】:

    我宁愿使用递归函数:

    版本1:简而言之:

    public int companySize() {
        final int result = Integer.parseInt(JOptionPane.showInputDialog("Please input the company size"));
        return (result >= 4 && result <= 10) ? result : companySize();
    }
    

    版本 2:但您也可以使用静态常量

    /* don't be afraid of shared constants; sometimes they're very "useful idiots" */
    /*public*/ static final int MIN = 4;
    /*public*/ static final int MAX = 10;
    /*public*/ static final String MESSAGE = "Please input the company size";
    
    public int companySize() {
        final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
        return (result >= MIN && result <= MAX) ? result : companySize();
    }
    

    版本 3: 如果您想显示错误消息:

    /* don't be afraid of shared constants; sometimes they're very "useful idiots" */
    /*public*/ static final int MIN = 4;
    /*public*/ static final int MAX = 10;
    /*public*/ static final String MESSAGE = "Please input the company size";
    /*protected*/ static final String ERROR_MESSAGE = "Please input a valid company size";
    
    public int companySize() {
        final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
        return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
    }
    
    private int companySize(String errorMessage) {
        final int result = Integer.parseInt(JOptionPane.showInputDialog(errorMessage));
        return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
    }
    

    public static void main(String[] args) {
        UserInput getResult = new UserInput();
        int company_size =  getResult.companySize();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 2020-11-26
      • 2021-07-27
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2015-07-03
      相关资源
      最近更新 更多