【发布时间】:2018-10-18 05:07:20
【问题描述】:
我想将输入 作为 Java 中的字符串 并使用 try catch 限制用户不输入整数。
import java.util.*;
public class trycatch {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String a;
System.out.println("\n\nEnter the name");
try {
a=sc.nextLine();
System.out.println("You name is "+a);
}
catch(InputMismatchException b) {
System.out.println("There is problem with your input");
}
}
}
【问题讨论】:
-
从扫描仪中你永远不会得到整数格式的输入。它始终是字符串,如果你愿意,你必须将它转换为整数。即 '123' 可以转换为 123
标签: java