import java.util.*;
class Student1
{
String name;
int age;
public Student1(){ //初始化实例属性
Scanner input=new Scanner(System.in);
System.out.print("请输入你的姓名:");
name=input.next();
System.out.print("请输入你的年龄:");
age=input.nextInt();
}
public static void main(String[] args){
Student1 stu=new Student1();
if (stu.age>=16) //判断年龄的属性值是否正确
{
stu.introduceSelf(); //正确就调用自定义的方法介绍名字和年龄
}else{
System.out.println("你输入的年龄有错误(应在16岁以上)");
} //错的就输出这行代码,并退出
}
public void introduceSelf(){
System.out.println("我的叫"+this.name+",年龄是"+this.age);
}
}
这是我用构造方法做的
没问题吧。。。好像是
class Student1
{
String name;
int age;
public Student1(){ //初始化实例属性
Scanner input=new Scanner(System.in);
System.out.print("请输入你的姓名:");
name=input.next();
System.out.print("请输入你的年龄:");
age=input.nextInt();
}
public static void main(String[] args){
Student1 stu=new Student1();
if (stu.age>=16) //判断年龄的属性值是否正确
{
stu.introduceSelf(); //正确就调用自定义的方法介绍名字和年龄
}else{
System.out.println("你输入的年龄有错误(应在16岁以上)");
} //错的就输出这行代码,并退出
}
public void introduceSelf(){
System.out.println("我的叫"+this.name+",年龄是"+this.age);
}
}
这是我用构造方法做的
没问题吧。。。好像是