键盘录入时判断数据类型

public class Work4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入姓名:”);
String name = sc.nextLine();
System.out.println(“请输入年龄:”);
Person p = new Person();
p.setName(name);
while (true){
//以下为判断方法
try {
int i = Integer.parseInt(sc.nextLine());
p.setAge(i);
System.out.println(“输入完毕,信息为:”+p.toString());
break;
} catch (NumberFormatException e) {
System.out.println(“您输入的年龄格式有误,请重新输入!”);
}

    原文作者:Nature_____
    原文地址: https://blog.csdn.net/Nature_____/article/details/102632454
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞