当用户输入" 0"时,显示错误消息,无法退出"错误"信息

时间:2014-07-29 04:23:02

标签: java

我对如何解决此问题有任何想法?我告诉用户输入他想要平均的数字量。当然,正如您在代码中看到的,如果他输入0或负数,我希望它标记用户输入另一个非0或负数的数字。问题是,一旦用户输入0或负数,它就会陷入该状态,我必须终止该程序。

帮助?

import javax.swing.JOptionPane;

public class TestProgTres 
{
    public static void main(String[] args) 
    {
         //Variable Declaration
         String ShowSome;
         String ShowSomeAgain;
         int z           = 0;
         double avg      = 0;
         double totalamt = 0;

         ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers you would like to average"); 
         double AllNumbers = Double.parseDouble(ShowSome);

         while (AllNumbers < 1)
         {
             JOptionPane.showInputDialog("You cannot enter a negative or a 0.  Enter the amount of numbers you would like to average");
             AllNumbers = Double.parseDouble(ShowSome);
         }//end while

             if (AllNumbers > 0)
             {
                 double Numbers [] = new double [(int) AllNumbers];

                 for (z = 0; z < Numbers.length; z++)
                 {
                     ShowSomeAgain= JOptionPane.showInputDialog("Enter number " + (z + 1));
                     Numbers[z]=Double.parseDouble(ShowSomeAgain);

                     totalamt += Numbers[z];
                     avg = totalamt/AllNumbers;
                 }
             }

              JOptionPane.showMessageDialog(null, "The  of the numberes entered is " + avg);
        }//end main
}// end class

3 个答案:

答案 0 :(得分:3)

嗯,它不是最干净的,但这有一个相当重要的错误,

ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers "
    + "you would like to average"); 
double AllNumbers = Double.parseDouble(ShowSome);

while (AllNumbers < 1)
{
  JOptionPane.showInputDialog("You cannot enter a negative or a 0. "
      + "Enter the amount of numbers you would like to average");
  // HERE!
  AllNumbers = Double.parseDouble(ShowSome);
}//end while

您需要提示并更新ShowSome

while (AllNumbers < 1)
{
  // HERE!
  ShowSome = JOptionPane.showInputDialog("You cannot enter a negative or a 0. "
      + " Enter the amount of numbers you would like to average");
  // Get it again.
  AllNumbers = Double.parseDouble(ShowSome);
}//end while

此外,Java命名约定会以小写字母开头命名变量。使用showSome会更容易阅读ShowSome看起来像一个类名。与allNumbers相同。

答案 1 :(得分:2)

在循环内移动以下代码,以便在用户输入错误输入时提示输入,并在获得所需内容时中断循环。

     //Declare ShowSome and AllNumbers outside loop
     while (true){
          try{
            ShowSome = JOptionPane.showInputDialog("Enter....."); 
            AllNumbers = Double.parseDouble(ShowSome);
            //May Throw exception for invalid input So be careful with this 
            if(AllNumbers>=1)break;
            JOptionPane.showInputDialog("You cannot enter a negative or a 0"); 
          }catch(NumberFormatException e){
          JOptionPane.showInputDialog("Not a valid Number!"); 
          //Not Recommended to swallow the exception
          }
     }

其次没有在执行此操作后检查if (AllNumbers > 0)的含义,因为上面的代码将提示,直到用户输入错误的输入,因此您肯定会得到>=1的正确值。

您应该使用try-catch机制来避免Exception无效输入。

此外,您应将AllNumbers声明为integer以避免强制转换。

答案 2 :(得分:2)

基本上,您想要至少一次要求用户输入一个值,那么为什么不将整个工作负荷降低到单个do-while循环,例如......

double AllNumbers = 0;
do {

    String ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers you would like to average"); 
    AllNumbers = Double.parseDouble(ShowSome);
} while (AllNumbers < 1);

您在当前循环中遇到的主要问题是您忽略JOptionPane的返回值并不断解析之前输入的内容...

// ShowSome is now set to, let's say 0...
ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers you would like to average"); 
// AllNumbers  is now 0
double AllNumbers = Double.parseDouble(ShowSome);

while (AllNumbers < 1)
{
     // Prompting for a value, but you are ignoring it...
     JOptionPane.showInputDialog("You cannot enter a negative or a 0.  Enter the amount of numbers you would like to average");
     // Parsing ShowSome which is still 0 (as an example)...
     AllNumbers = Double.parseDouble(ShowSome);
 }//end while