我試圖循環異常,但由於某種原因它不是給我的選項重新寫我的掃描文件:循環永遠
我不知道如何使用的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;
}
}
如果任何人有如何循環此不同的任何想法我都是耳朵。
你爲什麼要在{}內包裹do-while? – 2009-11-19 12:35:59
'while(error == true)' - >'while(error)' – JRL 2009-11-19 12:37:44
while(error == true)會更習慣性寫入while(error) – Henry 2009-11-19 12:39:17