2013-03-14 23 views
2

嘿家伙,所以我一直试图回答这个问题几个小时:
编写一个程序,要求用户输入一组浮点值。当 用户输入的值不是数字时,请给用户第二次输入值 。两次机会后,退出阅读输入。添加所有正确指定的值,并在用户输入数据时打印总和。使用异常处理来检测 不正确的输入。在Java中处理异常 - 如何给用户另一个机会?

我试过几个不同的东西,但我总是有同样的问题。一旦输入了不是数字的东西,程序就会输出提示输入另一个输入的消息,但是没有给出机会,也就是说,在输入1个不正确的输入后,它会打印该消息并直接跳转到打印总和。我能做的最好的是在下面,我只是不确定如何解决这个问题。任何帮助是极大的赞赏。

import java.util.Scanner; 
import java.util.InputMismatchException; 
public class q6{ 
public static void main(String[] args){ 
    Scanner in = new Scanner(System.in); 
    boolean firstChance = true; 
    boolean secondChance = true; 
    double sum = 0; 
    while (secondChance){ 
     try{ 
      while (firstChance){ 
       try{ 
        System.out.print("Please enter a number: "); 
        double input = in.nextDouble(); 
        sum = sum + input; 
       } 
       catch (InputMismatchException ex){ 
        firstChance = false; 
       } 
      System.out.print("Please enter a number to continue or something else to terminate: "); 
      double input = in.nextDouble(); 
      sum = sum + input; 
      firstChance = true; 
      } 
     } 
     catch (InputMismatchException e){ 
      secondChance = false; 
     } 
    } 
    System.out.print("The sum of the entered values is " + sum); 
} 
} 
+0

而不是读一个'double',读一个'String'并使用'Double.parseDouble(String)'。另外,如果你的'firstChance'在try-catch块后面是'true',你应该打破'while循环'。 – 2013-03-14 06:07:27

+0

看着你的问题,你似乎知道逻辑,但它没有反映在代码中。我建议用测试输入干运行此代码,并尝试自己找出问题。您只需确保在给出错误消息 – Shurmajee 2013-03-14 06:09:04

回答

0

我只是不知道如何处理这个问题

伪代码可能如下:

BEGIN 

    MAX_INPUT = 2; 
    i = 0; 

    WHILE i < MAX_INPUT 

     TRY 
      num = GET_NUM(); 
     CATCH 
      continue; 
     FINALLY 
      i++ 

    END WHILE 

END 
+0

*使用异常处理来检测不正确的输入*我想你忘了阅读这部分的问题。 – 2013-03-14 06:15:25

+0

@LuiggiMendoza:哦......是的。感谢您的纠正。 – Azodious 2013-03-14 06:16:57

+0

感谢这个工作,以及在发现异常后重新初始化扫描器的其他建议。 – Reddy 2013-03-14 16:50:23

0

当你遇到一个“坏”输入您需要休息循环while。然后,你需要重新设置firstChance真正,这样你就可以访问第二while,您还需要计数的尝试次数的计数器(我把它命名为chances):

int chances = 0; 
while (secondChance){ 
    firstChance = true; 
    try{ 
     while (firstChance){ 
      try{ 
       System.out.print("Please enter a number: "); 
       double input = in.nextDouble(); 
       sum = sum + input; 
      } 
      catch (InputMismatchException ex){ 
       chances ++; 
       in = new Scanner(System.in); 
       firstChance = false; 
      } 
      if(!firstChance && chances < 2) 
       break; 
      if(chances >= 2) { 
       System.out.print("Please enter a number to continue or something else to terminate: "); 
       double input = in.nextDouble(); 
       sum = sum + input; 
       firstChance = true; 
      } 
     } 
    } 
    catch (InputMismatchException e){ 
    secondChance = false; 
    } 
} 
+0

后,程序就会返回到初始提示,这不是程序的期望工作。再次阅读问题。 – uba 2013-03-14 06:21:49

+0

谢谢,修正.. – Maroun 2013-03-14 06:26:53

0

由于将输入解析为double是不成功的,因此扫描器不会超出给定的输入。从javadoc Scanner

将输入的下一个标记扫描为双精度。如果下一个标记不能被翻译为 有效的double值,则此方法将抛出 InputMismatchException。 如果翻译成功,则扫描仪 前进超过匹配的输入。

由于翻译不成功,所以没有前进。因此,在catch块中,您可以调用in.next()来跳过令牌。

0

你可以使用一个整数计数器,而不是布尔变量firstChance和secondChance的并做类似的事情:

int attempts = 0; 
while(attempts < 2) { 
    try { 
     //Get user input - possible exception point 
     //Print sum 
    } catch(InputMismatchException e) { 
     attempts++; 
     //Continue? 
    } 
} 
0
import java.util.*; 

public class q6 { 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     double inputNumber, sum = 0.0; 
     int correctCount = 0, wrongCount = 0; 

     while(wrongCount <=1) { 
      System.out.println("Please enter a numeric floating value:"); 
      try { 
       inputNumber = in.nextDouble(); 
       correctCount++; 
       sum += inputNumber; 
       if(correctCount >= 2) 
        break; 
      } catch(InputMismatchException e) { 
       wrongCount++; 
       in = new Scanner(System.in); 
       continue; 
      } 
     } 
     System.out.println(sum); 
    } 
} 
0

正如我的评论所述,为了不处理Scanner错误读取,最好使用Scanner#nextLine()并读取数据为String,然后尝试使用Double#parseDouble(String)解析为double,因为此方法已经抛出NumberFormatException而您可以处理这个错误。此外,它会更好,你的代码可以处理超过1个请求(如果你的老师或其他人要求这样做):

final int REQUEST_TIMES = 2; 
double sum = 0; 
for(int i = 1; i <= REQUEST_TIMES; i++) { 
    while (true) { 
     try { 
      if (i < REQUEST_TIMES) { 
       System.out.print("Please enter a number: "); 
      } else { 
       System.out.print("Please enter a number to continue or something else to terminate: "); 
      } 
      String stringInput = in.nextLine(); 
      double input = Double.parseDouble(stringInput); 
      sum += input; 
     } catch (NumberFormatException nfe) { 
      System.out.println("You haven't entered a valid number."); 
      break; 
     } 
    } 
} 
0

的逻辑是读取输入一次,如果它是正确的,那么显示器总和,否则再次读取输入并在输入正确时显示总和。

public class q6 { 

    static int trial = 0; 

    public static void main(String[] args) { 
     double sum = 0; 

     while (trial <= 2) { 
      Scanner in = new Scanner(System.in); 
      try { 
       trial++; 
       System.out.print("Please enter a number: "); 
       double input = in.nextDouble(); 
       sum = sum + input; 
       trial=3; 
      } catch (InputMismatchException ex) { 
       trial++; 
       if (trial == 4){ 
        System.out.println("You have entered wrong values twice"); 
       } 
      } 
     } 
     if (trial <= 3){ 
      System.out.print("The sum of the entered values is " + sum); 
     } 
    } 

上述计划仅仅是一个粗略的想法,可以提高这个以适应您的需求。

+0

出于某种原因,在循环外初始化扫描仪将无法使用多次尝试! – Rakesh 2013-03-14 06:49:55