import java.util.Scanner;
public class Questioner {
Scanner scanner = new Scanner(System.in);
boolean condition;
int tempInt;
double tempDouble;
public Questioner()
{
condition = true;
}
public String stringInput(String text)
{
System.out.print(text);
return scanner.nextLine();
}
public int intInput(String text)
{
do
{
System.out.print(text);
try
{
tempInt = scanner.nextInt();
condition = false;
}
catch (java.util.InputMismatchException error)
{
System.out.println("Please use valid input.");
}
} while (condition == true);
return tempInt;
}
public double doubleInput(String text)
{
System.out.print(text);
try
{
return scanner.nextDouble();
}
catch (java.util.InputMismatchException error)
{
System.out.println("Please use valid input.");
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,它在一次错误后无限循环捕获.我怎样才能让它在捕获后回到尝试?boolean condition声明正确,没有编译错误或任何东西.这个代码中的其余代码有点混乱,因为我正在等待重新尝试的答案.
| 归档时间: |
|
| 查看次数: |
7122 次 |
| 最近记录: |