java try catch InputMismatchException问题,当输入字符时一直循环
importjava.util.InputMismatchException;importjava.util.Scanner;publicclasstest{/***@p...
import java.util.InputMismatchException;
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//当输入字符时会一直循环,怎么办
int c;
Scanner input=new Scanner(System.in);
while(true)
{
try{
System.out.println("请输入c:");
c=input.nextInt();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("输入命令有误,请按照提示输入数字命令");
//continue;
}
}
}
} 展开
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//当输入字符时会一直循环,怎么办
int c;
Scanner input=new Scanner(System.in);
while(true)
{
try{
System.out.println("请输入c:");
c=input.nextInt();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("输入命令有误,请按照提示输入数字命令");
//continue;
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询