面向对象第三章课后作业

package Student;


//(题目1:编写一个Student1代表学员代表学员,具有属性:姓名和年龄,其中年龄不能小于16岁,否则输出错误信息。)


import java.util.Scanner;              


public class Student1 {
String name = "";                  // 姓名
int age = 0;                       // 年龄


public void staAge() {
if (age > 16) {                 // 判断年龄是否满16岁
System.out.println("自我介绍:\n我的姓名是:" + name + "\n我的年龄是:" + age);
} else {
System.out.println("年龄不符");
}
}


public static void main(String[] args) {
Student1 stu = new Student1();
Scanner input = new Scanner(System.in);
System.out.print("请输入姓名:");
stu.name = input.next();
System.out.print("请输入年龄:");
stu.age = input.nextInt();
stu.staAge(); // 调用staAge方法输出结果
}

}



********************************************************************************



package Student;
//(题目2:编写一个Student2类,代表学员,具有属性:姓名和年龄,性别和专业。具有方法:自我介绍,负责输出该学员的信息。)


import java.util.Scanner;


public class Student2 {
String name; // 姓名
int avg; // 年龄
String sex; // 性别
String profession; // 专业


public void introduce() { // 输出自我介绍的方法
System.out.println("自我介绍:\n" + "我的姓名是:" + name + "\n我的年龄是:" + avg + "\n我的性别是:" + sex + "\n我的专业是:" + profession);
}


public Student2(String name, int avg) {
this.sex = "男";
this.profession = "JAVA";
this.name = name;
this.avg = avg;
}


public Student2(String name, int avg, String sex, String profession) {
this.name = name;
this.avg = avg;
this.sex = sex;
this.profession = profession;
}


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = input.next();
System.out.print("请输入年龄:");
int avg = input.nextInt();
System.out.print("请输入性别:");
String sex = input.next();
System.out.print("请输入专业:");
String profession = input.next();


Student2 stu = new Student2(name, avg); // 创建方法并传参
Student2 stu2 = new Student2(name, avg, sex, profession);


stu.introduce(); // 调用方法输出信息
stu2.introduce();


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值