2014-11-24 102 views
0

嗨我有麻烦的家庭作业。该程序使用两种不同的方法来平均和显示用户定义的一组数字。我想到了所有这些,但是我在检查错误时遇到了困难。我不希望用户能够说,他们想要在开始时平均负数或零数量的数字,所以我试图使用if/else语句和do while循环来向用户显示错误消息并让他们有机会再次尝试。使用do/while循环错误检查用户输入

当用户输入1以在输入无效数字后再次尝试该程序时,程序允许用户再次尝试。但是,一旦他们正确地输入了所有内容并完成程序,程序就会重新开始。一旦程序正确完成,我希望程序结束。

任何帮助,将不胜感激。谢谢!

public static void main(String[] args) 
{ 
    //Defining the variables in main method 
    int inputNumber; 
    int repeat = 0; 

    //Creating the array and checking for negative or no numbers using do..while and if..else 
    do 
    { 
     String aStr = JOptionPane.showInputDialog(null, "How many numbers would you like to be averaged?"); 
     inputNumber = Integer.parseInt(aStr); 

      if(inputNumber <= 0) 
      { 
       String errorStr = JOptionPane.showInputDialog(null, "Cannot be a negative number or a zero. Press 1 to try again."); 
       repeat = Integer.parseInt(errorStr); 
      } 
      else 
      { 
       double[] array = new double[inputNumber]; 
       displayAverage(average(array)); 
      } 
    } while (repeat == 1); 
} // end main 

// Creating a method called "average" that calculates and returns the average to main 
public static double average(double [] methodArray) 
{ 
    // Defining variables in average method 
    int index; 
    double total = 0; 
    double average; 

    // Taking user inputed numbers and adding them up 
    for(index = 0; index < methodArray.length; index++) 
    { 
     String bStr = JOptionPane.showInputDialog(null, "Enter number " + (index + 1)); 
     methodArray[index] = Double.parseDouble(bStr); 

     total = total + methodArray[index]; 
    } 
    // Calculating the average 
    average = total/index; 
    return average; 
} //end average method 

// Creating a method called "displayAverage" that displays the average in a dialog box 
public static void displayAverage(double returnedAverage) 
{ 
    JOptionPane.showMessageDialog(null, "The average of all your numbers is " + returnedAverage); 
} 

} //结束类

+0

究竟是什么问题?你无法理解“do-while”的含义?或者你在检查输入的有效性方面有问题?或者你有超过1个输入的问题? – 2014-11-24 01:51:41

+0

我遇到的问题是与输入的有效性检查有关。我似乎要么不正确地使用do-while要么从某些答案/评论的外观来看它位于不正确的位置。 – detomaso55 2014-11-24 03:23:44

+0

下次如果你可以写下一个只显示问题的小程序(calculateAverage和displayAverage简直不相关),那么它会好得多,并且2.明确你遇到的预期行为和有问题的行为。在很多情况下,通过做1,你可以自己找出解决方案。 – 2014-11-24 03:29:36

回答

1

看来,用户再次尝试并进入有效输入后,你永远不会改变的repeat如此循环将永远不会退出的价值。在else如果你的if-else在循环内,你需要重新指定repeat到1以外的东西,所以循环可以退出!

0

要循环直到获得有效的输入,请尝试如下所示:仅当输入有效时,才将validInput布尔值设置为true。

boolean validInput = false; 
while (!validInput) { 
    //get input from user 
    if (userInputIsValid) { //however this is done 
     validInput = true; 
    } 
} 
+0

这个问题具体是关于正确使用do/while的问题。虽然这个代码是相同的,但它不关于OP的要求。 – hfontanez 2014-11-24 03:15:36

+0

@hfontanez我明白这个问题是关于提示用户输入,获取输入,如果输入不好,则循环,如果它很好,则打破循环。 OP恰好使用了“do-while循环”,但是不必要的。 – yts 2014-11-24 03:19:37

+0

@hfontanez yts是正确的。对不起,我应该更清楚。我不一定非要使用do-while循环,它只是我目前知道在检查有效输入时如何重复我的程序的唯一方法。在我尝试使用do-while之前,我只使用if-else语句来检查有效的输入,但问题在于我的程序只是通知用户错误,然后终止程序。 – detomaso55 2014-11-24 03:31:14

0

你必须做这样的事情:

boolean repeat = false; 

    do 
    { 
     String aStr = JOptionPane.showInputDialog(null, 
       "How many numbers would you like to be averaged?"); 
     try 
     { 
      inputNumber = Integer.parseInt(aStr); 
      if (inputNumber <= 0) 
      { 
       JOptionPane.showMessageDialog(null, 
        "Input must be a number greater than zero. Try again.", 
        "ERROR: Invalid input", JOptionPane.ERROR_MESSAGE); 
       repeat = true; 
      } 
      else 
      { 
       double[] array = new double[inputNumber]; 
       displayAverage(average(array)); 
      } 
     } 
     catch (NumberFormatException e) 
     { 
      JOptionPane.showMessageDialog(null, 
        "Input must be a numeric string. Try again", 
        "ERROR: Invalid input", JOptionPane.ERROR_MESSAGE); 
      repeat = true; 
     } 

    } while (repeat); 

需要在try/catch来处理无效的数字字符串像一个空白的文本字段,或其他非数字字符。

您可能还想包含其他逻辑来处理取消按钮。

0

虽然有另外一个答案给出了正确的答案(我相信是这样),但我想详细说明一下。

这是伪代码:

do { 
    inputNumber = getInput(); 
    if(inputNumber <= 0) { 
     displayErrorMessage(); 
     repeat = 1;  // your logic makes no sense when people input non-1 
    } else { 
     calculateAverage(inputNumber); 
    }  
} while (repeat == 1) 

的问题是相当明显的:一旦你得到了一个无效inputNumber,设置repeat = 1造成do-while循环再继续。然而,之后,即使用户输入有效的inputNumber,repeat保持为1,并且每次它在while(repeat ==1)中检查时,它仍将评估为真。

我强烈建议您学习以基本方式使用调试器,以便您可以跟踪代码,并且即使您输入有效数字,您也可以通过查看它保持循环来轻松识别问题,因为repeat始终为1

另一个建议是重写一下你的代码,使其更容易理解。相反,在命名将重复这意味着什么,你可以写的东西:

boolean inputIsValid= false; 
do { 
    inputNumber = getInput(); 

    inputIsValid= verifyInput(inputNumber); 

    if (inputIsValid) { 
     calculateAverage(inputNumber); 
    } else { 
     displayErrorMessage(); 
    } 
} while (! inputIsValid) 

流量,恕我直言,更直观。

这只是第一步。你甚至可以更好地包裹得输入逻辑在一个单独的方法,使主要的逻辑是这样的:

inputNumber = getInput(); 
calculateAverage(inputNumber); 

,你把循环中getInput()。看起来更清楚不是吗?