日期:2014-05-20 浏览次数:20677 次
static void scannerme(){
Scanner kScanner =new Scanner(System.in);
System.out.print("输入一段文字:");
String nInput=kScanner.nextLine();
System.out.println("您输入的是:"+nInput);
kScanner.reset();
try {
System.out.print("输入一个数字:");
int d=kScanner.nextInt();
System.out.println("您输入的数字是:"+d);
} catch (Exception e) {
// TODO: handle exception
System.out.print("输入一个数字:");
kScanner.reset();
int d=kScanner.nextInt();
System.out.println("您输入的数字是:"+d);
}
}
public static void main(String[] args) {
Scanner kScanner = new Scanner(System.in);
System.out.print("输入一段文字:");
String nInput = kScanner.nextLine();
System.out.println("您输入的是:" + nInput);
kScanner.reset();
while (true) {
try {
System.out.print("输入一个数字:");
int d = kScanner.nextInt();
System.out.println("您输入的数字是:" + d);
break;
} catch (Exception e) {
kScanner.next();
System.out.println("输入格式不对,请重新输入");
}
}
}