2013-07-15 127 views
1

这是我目前的代码。 我想要它做什么,是接受多达10个数字在数组然后做,并显示他们的一些数学。我设法做的是捕捉错误,然后停止程序。 我想要它做的是保持程序运行,直到用户正确输入一个整数。 我设法为我的y/n字符串做类似的事情,但我不知道如何做整数数组。如何重复,直到用户输入一个整数?

public static void main(String[] args) { 
    Scanner input = new Scanner (System.in); 
    int i=0, numberlist[] = new int [10]; 
    String yn=null; 

     while (i < 10) 
    { 
     try { 
      System.out.print("Please enter your number\n"); 
      numberlist[i]=input.nextInt(); 
      System.out.print("Would you like to enter another number? (y/n)\n"); 
      yn=input.next(); 
      i++; 
         if (i==10) 
          {System.out.println("You reached the maximum amount of numbers\n"); 
          break;} 

         if (yn.equals("n")) 
          break; 

         else if (!yn.equals("y")) 

          while (true) 
           {System.out.print("Please only enter a 'y' for yes or 'n' for no next time.\nDo you understand? Type 'y' to continue\n"); 
           yn=input.next(); 
            if (yn.equals("y")) 
             break; 
           } 


      }catch (Exception e){System.out.println("Please enter the correct number(integers only) next time.");} 
} 

    int max=numberlist[0], min=numberlist[0], numlength = i, sum=0; 
    float average; 

    for(i = 0; i < numlength; i++) { 
     if(numberlist[i] > max) 
      max = numberlist[i];    
    } 

    for(i = 0; i < numlength; i++) { 
     if(numberlist[i] < min) 
      min = numberlist[i]; 
    } 

    for(i = 0; i < numlength; i++) { 
     sum=numberlist[i]+sum; 
    } 
    average = (float)sum/(float)numlength; 

    System.out.println("Your Sum is: "+sum); 
    System.out.println("Your Average is: "+average); 
    System.out.println("Your Maximum is: "+max); 
    System.out.println("Your Minimum is: "+min); 
} 
+0

首先,不要捕捉异常。 –

回答

1

移动对while循环内的数字的错误处理,以便任何异常都不会中断流程并结束程序。

while (i < 10) { 
    try { 
     System.out.print("Please enter your number\n"); 
     numberlist[i] = input.nextInt(); 
     System.out.print("Would you like to enter another number? (y/n)\n"); 
     yn = input.next(); 
     i++; 
     if (i == 10) { 
      System.out.println("You reached the maximum amount of numbers\n"); 
      break; 
     } 

     if (yn.equals("n")) 
      break; 
     else if (!yn.equals("y")) 
      makeUserUnderstand(input, 
        "Please only enter a 'y' for yes or 'n' for no next time."); 

    } catch (InputMismatchException e) { 
     makeUserUnderstand(input, 
       "Please enter the correct number (integers only) next time."); 
    } 
} 

我已经搬出普通“你明白吗?”部分转化为方法。

private static void makeUserUnderstand(Scanner input, String msg) { 
    while (true) { 
     System.out.println(msg); 
     System.out.println("Do you understand? Type 'y' to continue\n"); 
     if (input.next().equals("y")) 
      break; 
    } 
} 
+0

我试着简单地切换尝试的位置和我原来的代码。当我输入一个非整数时,它给了我错误,告诉我再次输入一个数字,但它不会等待输入并永远循环。为什么会发生这种情况,我该如何解决这个问题? –

+0

你可能用大括号犯了一个错误。或者用你当前的代码更新问题,或者简单地复制我的* while循环和辅助方法。我测试过它,它工作。 –

+0

好的我更新了它。 –

0

首先,不要捕捉异常。您应该只捕获您关心和知道可能发生在您的代码中的特定异常。任何其他异常都表明存在严重问题,通过捕获它们,您可能会意外地压制指示需要注意的错误的重要信息。

这样说,你可以通过只包装读取输入的代码来使你的try块变小来解决你的问题。另外,创建一个循环来检查指示是否发生错误的标志。当输入解析为整数时发生错误时,可以在catch块中设置该标志。

如果您在将我的描述转换为代码时遇到困难,请随时询问有关的详细信息。

相关问题