import java.util.Scanner; import java.util.regex.Pattern; public class Test2 { public static void main(String []args){ Scanner input=new Scanner(System.in); System.out.println("欢迎访问本校教师学生信息录入系统! "); System.out.print("请输入要录入的姓名: "); String name=input.next(); System.out.print("请输入您的年龄: "); int age=input.nextInt(); System.out.print("请输入您的性别: "); String gender=input.next(); System.out.print("请输入要录入的角色:(1.教师 2.学生) "); switch(input.nextInt()){ case 1: System.out.print("请选择教师的授课方向:(1.java方向 "+"2..NET方向) "); String majorFiled=null; if(input.nextInt()==1) { majorFiled="Java方向"; } else{ majorFiled=".NET方向"; } Teacher teacher=new Teacher(); teacher.name=name; teacher.age=age; teacher.majorField=majorFiled; teacher.print(); break; case 2: System.out.print("请选择学生的年级:(1、大一 2、大二 3、大三) "); String grade=null; int gradeInt=input.nextInt(); if(gradeInt==1) grade="大一"; else if (gradeInt==2) { grade="大二"; } else { grade="大三"; } Student student=new Student(); student.name=name; student.grade=grade; student.print(); } } }
public class Student { //定义一个学生类 String name; int age; char gender; String grade; public void print(){ System.out.println("我是"+name+",我的年龄是"+age+"岁,我的性别是"+gender+",目前我上"+grade); } }
public class Teacher { String name; String majorField; int age; char gender; public void print(){ System.out.println("我是"+name+",我的年龄是:"+age+"岁,我的性别是"+gender+",我的授课方向是:"+majorField); } }