2009-11-19 139 views
0

我试图循环异常,但由于某种原因它不是给我的选项重新写我的扫描文件:循环永远

我不知道如何使用的BufferedReader所以这就是为什么我使用这个。任何线索?

这里是我的标准类和我的方法

package arrayExceptionsWithInput; 
import java.util.*; 
public class GetThoseNumbersBaby { 

    int firstInt; 
    int secondInt; 
    boolean error; 
    Scanner yourNumbers = new Scanner(System.in); 

    public void findNumbers() 
    { 
     System.out.println(" please enter your first number"); 
     firstInt = yourNumbers.nextInt(); 
     System.out.println(" pleas enter your second number"); 
     secondInt = yourNumbers.nextInt(); 
     int finalInt = (firstInt+secondInt); 
     System.out.println("total is: " + finalInt); 
    } 
} 

这是我的不同之处主要类与一环Implemeted一个:

package arrayExceptionsWithInput; 
import java.util.*; 

public class InputException 
{ 
    public static void main(String[] args) 
    { 
     boolean error = false; 
     GetThoseNumbersBaby zack = new GetThoseNumbersBaby(); 


     { 
      do { 
       try 
       { 
        zack.findNumbers(); 
       } 
       catch(InputMismatchException Q) 
       { 
        System.out.println(" one of your integers was incorrect, please try again"); 
        Q.printStackTrace(); 
        error = true; 
       } 
      } while (error == true); 
     } 
     error = false; 
    } 
} 

如果任何人有如何循环此不同的任何想法我都是耳朵。

+0

你为什么要在{}内包裹do-while? – 2009-11-19 12:35:59

+0

'while(error == true)' - >'while(error)' – JRL 2009-11-19 12:37:44

+0

while(error == true)会更习惯性写入while(error) – Henry 2009-11-19 12:39:17

回答

0

我决定也摆脱方法类 的,只是把该方法进入试异常区。

扫描仪使用了一个.nextLine,它似乎是固定的。

这看起来好吗?

package arrayExceptionsWithInput; 
import java.util.*; 
public class InputException 
{ 
public static void main(String[] args) 
{ 
boolean error = false; 
int firstInt; 
int secondInt; 
Scanner yourNumbers = new Scanner(System.in); 
{ 
    do{ 
      try 
      { 

        System.out.println(" please enter your first number"); 
        firstInt = yourNumbers.nextInt(); 
        yourNumbers.nextLine(); 
        System.out.println(" pleas enter your second number"); 
        secondInt = yourNumbers.nextInt(); 
        yourNumbers.nextLine(); 
        int finalInt = (firstInt+secondInt); 
        System.out.println("total is: " + finalInt); 
        yourNumbers.nextLine(); 

      } 
      catch(InputMismatchException Q) 
      { 

       Q.printStackTrace(); 
       System.out.println(" one of your integers was incorrect, please try again"); 
       error = true; 
       yourNumbers.nextLine(); 
      } 
     }while (error == true); 
    }error = false; 
} 
} 
+0

扫描仪保持答案,并没有休息,除非我把你的Numnbers.NextLine() – OVERTONE 2009-11-19 12:33:28

+0

更有用的,如果你编辑你的问题更新,而不是通过答案的方式添加更新。 – rsp 2009-11-19 12:34:04

+2

您仍然需要在循环中设置error = false,否则如果您确实遇到异常,则错误将被设置为true并且永远不会被清除。 – 2009-11-19 12:34:45

1

设置错误false 之前的操作。这样,如果用户得到正确的结果,你有正确的退出条件。

error = false; 
zack.findNumbers(); 
+0

这里downvote的原因是什么? – 2009-11-19 12:37:21

+0

对不起,还在使用太栈接口。我不知道我有足够的代表投票在帖子 – OVERTONE 2009-11-19 15:30:36

0

你循环而'错误'变量值'真'。然而,只有当抛出时(即当'捕获'块被执行时)它才变成'真''。控制流程没有达到它。