【问题标题】:Is it possible to merge these two do loops是否可以合并这两个 do 循环
【发布时间】:2015-06-24 13:31:57
【问题描述】:

我尝试将两个循环合并为一个 do 循环,但每次输入无效值时,它都不会提示我错误消息并要求再次输入该值。相反,它只是转到下一个提示语句。

do {
    try {
        dependents = Integer.parseInt(JOptionPane.showInputDialog("number of dependents:"));
    }
    catch (NumberFormatException e) {
        dependents = MIN_DEPENDENTS - 1;
    }
    if (dependents < MIN_DEPENDENTS || dependents > MAX_DEPENDENTS) {
        JOptionPane.showMessageDialog(null, "Number of dependents must be between 0 and 9.");
    }
} while (dependents < MIN_DEPENDENTS || dependents > MAX_DEPENDENTS);

do {
    try {
        income = Double.parseDouble(JOptionPane.showInputDialog("amount of income:"));
    }
    catch (NumberFormatException e) {
        income = MIN_INCOME - 1;
    }
    if (income < MIN_INCOME || income > MAX_INCOME) {
        JOptionPane.showMessageDialog(null, "income must be between $0 and $999,999.");
    }
} while (income < MIN_INCOME || income > MAX_INCOME);

【问题讨论】:

  • @WhyCry 依次是两个循环。
  • @BilltheLizard 我认为问题在于 OP 不想要 2 个循环,而是想要 1 个循环来完成这两个循环所做的一切。
  • 我不会合并它们,因为据我所知它们会做不同的事情
  • 我看到你在这些“循环”中所做的只是检查用户输入。这应该以完全其他方式完成。对于 JOptionPane,请参见此处:stackoverflow.com/questions/13055107/…
  • 合并这些循环没有意义。

标签: java


【解决方案1】:

不,但您可以创建某种 GetInput 函数并传入 min、max、promptText 和 errorText。这样可以节省您复制代码的时间。

dependents = getInput(MIN_DEPENDENTS, MAX_DEPENDENTS,"number of dependents:","Number of dependents must be between 0 and 9.")
income = getInput(MIN_INCOME,MAX_INCOME,"amount of income:","income must be between $0 and $999,999.")

private double getInput(double min, double max, String promptText, String errorText) {
   double result = 0.0;
   do {
         try {
            result = Double.parseDouble(JOptionPane.showInputDialog(promptText));
         }
         catch (NumberFormatException e) {
            result = min - 1;
         }

         if (result < min || result > max) {
            JOptionPane.showMessageDialog(null, errorText);
         }
   } while (result < min || result > max);

   return result;
}

【讨论】:

  • Java 方法参数不再需要类型了吗? :P
【解决方案2】:

Dan,您需要在您的 catch 块中添加以下划线以显示错误消息并再次提示输入。

catch (NumberFormatException e) {        
                dependents = MIN_DEPENDENTS - 1; 
    JOptionPane.showMessageDialog(null, "Number of dependents must be between 0 and 9.");

    dependents = Integer.parseInt(JOptionPane.showInputDialog("number of dependents:"));
}

【讨论】:

    【解决方案3】:

    另一个选项可以在数组中输入主列表,然后在while循环中使用它

    Object[][] input = {
                {"number of dependents", MIN_DEPENDENTS, MAX_DEPENDENTS},
                {"amount of income", MIN_INCOME, MAX_INCOME},
        };
    
        int index = 0;
        int value, min, max;
        do {
    
            Object[] inputDetails = input[index];
    
            String label = inputDetails[0].toString();
            min = Integer.valueOf(inputDetails[1].toString());
            max = Integer.valueOf(inputDetails[2].toString());
    
    
            try {
                value = Integer.parseInt(JOptionPane.showInputDialog(label));
            } catch (NumberFormatException e) {
                value = min - 1;
            }
            if (value < min || value > max) {
                JOptionPane.showMessageDialog(null, String.format("%s must be between %s and %s", label, min, max));
            } else {
                index++;
            }
    
        } while ((value < min || value > max) || index < input.length);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2020-11-23
      相关资源
      最近更新 更多