题
我试图循环异常,但由于某些原因,它没有给我重新写我的扫描文件的选项:
我不知道如何使用的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;
}
}
如果任何人有我如何循环这个不同洗耳恭听任何想法。
解决方案 2
我决定太摆脱方法类 而只是把方法进入试异常区。
使用扫描器之后的.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;
}
}
其他提示
设置错误假前的动作。这样,你有正确的退出条件,如果用户得到它的权利。
error = false;
zack.findNumbers();
您循环而 '错误'变量具有值 '真'。然而,它变得 '真'仅当被抛出(当 '捕捉'的块被执行,即)。控制流没有达到它。
不隶属于 StackOverflow