public class Test6 { public static class Person{ @SuppressWarnings("unused")
private String name;
private int age;
public void sayHello()
{
System.out.println("请输入您的名字:");
//初始化接收键盘输入的scanner变量
Scanner userName = new Scanner(System.in);
//获取用户输入的名字
String name = userName.nextLine();
//将获取到得名字来初始化name变量
this.name = name;
System.out.println("您输入的名字是:" + name + "\n请输入您的年龄:");
while(true)
{
//初始化接收键盘输入的scanner变量
Scanner userAge = new Scanner(System.in);
//获取用户输入的年龄
String strAge = userAge.nextLine();
int intAge;
//该异常会在输入的不是数字的情况下进入catch异常处理
try {
//将字符型变量强制转换成int型,如果字符型变量不是数字则会抛出异常
intAge = Integer.valueOf(strAge);
this.age = intAge;
System.out.println("您输入的年龄是:" + age);
if(age > 120)
{
System.out.println("您输入的年龄是" + age + "。您输入的年龄超出了正常范围,请核对重新输入:");
continue;
}
else
{
System.out.println("我叫" + name + ",今年" + age +"岁了。");
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("您输入的年龄不是年龄,请重新输入:");
}
}
}
}
public class Chinese extends Person{}
static Person person = null;
public static void main(String[] args)
{
person = new Person();
person.sayHello();
}
}