package com.whxiong.work03;
import java.util.Scanner;
public class Student {
String name; int age; Scanner input = new Scanner(System.in);public void setAge(String name, int age) {
System.out.println("请输入您的年龄:"); age = input.nextInt(); if (age < 16) { do { System.out.println("您输入的年龄不正确,请重新输入!"); System.out.println("请输入您的年龄:"); age = input.nextInt(); } while (age < 16); } this.age = age; }public void showStudent() {
System.out.println("大家好!我叫:" + name + ",今年" + this.age + "岁!"); System.out.println("欢迎您" + name + "学员!"); }}****************************************************************
package com.whxiong.work03;
import java.util.Scanner;
public class Student2 {
private String name; private int age; private String sex; private String major; Scanner input = new Scanner(System.in); //带参构造 public Student2(String name,int age) { this.name = name; this.age = age; this.sex = "男"; this.major = "Java"; }public void showSt1() {
System.out.println("大家好,我叫" + this.name + ",我是" + this.sex + "生!"); System.out.println("\t今年" + this.age + "岁,我的专业是" + this.major); } //有参构造 public Student2(String name, int age, String sex, String major) { this.name = name; this.age = age; this.sex = sex; this.major = major; }public void showSt2() {
System.out.println("大家好,我叫" + this.name + ",我是" + this.sex + "生!"); System.out.println("\t今年" + this.age + "岁,我的专业是" + this.major); }}**********************************************************************************
package com.whxiong.work03;
import java.util.Scanner;
public class Work03 {
/**
* @param args */ public static void main(String[] args) { Scanner input = new Scanner(System.in);System.out.println("第四题");
System.out.println("******************************"); Student st = new Student(); System.out.println("请输入您的姓名:"); st.name = input.next(); st.setAge(st.name,st.age); st.showStudent();System.out.println("\n");
System.out.println("第五题"); System.out.println("******************************"); System.out.println("请输入您的姓名:"); String name1 = input.next(); System.out.println("请输入您的年龄:"); int age = input.nextInt(); System.out.println("请输入您的性别:"); String sex = input.next(); System.out.println("请输入您的专业:"); String major = input.next(); Student2 st2 = new Student2(name1,age); st2.showSt1(); Student2 st3 = new Student2(name1,age,sex,major); st3.showSt2();}
}