首先我要说的是,我知道我的代码可以在函数上编写得更多,而且我知道我的变量比我需要的更多。这是针对入门Java类的学校作业,我正在尝试按照T的说明进行操作。
import java.util.Scanner;
public class UniversityTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String personType;
int userInput = 0;
Person[] all_people = new Person[100];
int count = 0;
String name;
String idNumber;
float GPA;
int admissionYear;
int expGrad;
int currYear;
String position;
int hireYear;
float salary;
String advisorName;
String researchArea;
int hours;
float rate;
while (count < 100){
System.out.println("Please enter the category of person: ");
personType = input.nextLine().toLowerCase();
switch (personType){
//if (personType.equals("student")){
case "student":
System.out.println("Enter the student's name: ");
name = input.nextLine();
System.out.println("Enter the student's ID number: ");
idNumber = input.nextLine();
System.out.println("Enter the student's GPA: ");
GPA = Float.parseFloat(input.nextLine());
System.out.println("Enter the student's admission year: ");
admissionYear = Integer.parseInt(input.nextLine());
System.out.println("Enter the student's expected year of graduation: ");
expGrad = Integer.parseInt(input.nextLine());
System.out.println("Enter the current year: ");
currYear = Integer.parseInt(input.nextLine());
Student new_student = new Student("Student", name, idNumber, GPA, admissionYear,
expGrad, currYear);
all_people[count] = new_student;
count++;
break;
//}
//else if (personType.equals("grad student")){
case "grad student":
System.out.println("Enter the grad student's name: ");
name = input.nextLine();
System.out.println("Enter the grad student's ID number: ");
idNumber = input.nextLine();
System.out.println("Enter the grad student's GPA: ");
GPA = Float.parseFloat(input.nextLine());
System.out.println("Enter the grad student's admission year: ");
admissionYear = Integer.parseInt(input.nextLine());
System.out.println("Enter the grad student's expected year of graduation: ");
expGrad = Integer.parseInt(input.nextLine());
System.out.println("Enter the grad student's position: ");
position = input.nextLine();
System.out.println("Enter the name of the grad student's advisor: ");
advisorName = input.nextLine();
System.out.println("Enter the grad student's area of research: ");
researchArea = input.nextLine();
System.out.println("Enter how many hours a week this grad student works: ");
hours = Integer.parseInt(input.nextLine());
System.out.println("Enter this grad student's hourly pay rate: ");
rate = Float.parseFloat(input.nextLine());
System.out.println("Enter the current year: ");
currYear = Integer.parseInt(input.nextLine());
GradStudent new_grad_student = new GradStudent("Grad Student", name, idNumber, GPA,
admissionYear, expGrad, currYear, position, advisorName, researchArea, hours, rate);
all_people[count] = new_grad_student;
count++;
break;
//}
//else if (personType.equals("faculty")){
case "faculty":
System.out.println("Enter the faculty member's name: ");
name = input.nextLine();
System.out.println("Enter the faculty member's ID number: ");
idNumber = input.nextLine();
System.out.println("Enter the faculty member's job: ");
position = input.nextLine();
System.out.println("Enter the year this faculty member was hired: ");
hireYear = Integer.parseInt(input.nextLine());
System.out.println("Enter this faculty member's annual salary: ");
salary = Float.parseFloat(input.nextLine());
Faculty new_faculty = new Faculty("Faculty", name, idNumber, position, hireYear, salary);
all_people[count] = new_faculty;
count++;
break;
//}
//else {
default:
System.out.println("Enter this person's name: ");
name = input.nextLine();
System.out.println("Enter this person's ID number: ");
idNumber = input.nextLine();
Person new_person = new Person(personType, name, idNumber);
all_people[count] = new_person;
count++;
break;
//}
}//END OF SWITCH
System.out.println("Press 1 to enter another user, or 0 to end.");
userInput = input.nextInt();
if (userInput == 0) {break;}
}//END OF WHILE LOOP
for(int i = 0; i < count; i++){
System.out.println(all_people[i].toString());
}
}//END OF MAIN
}
它适用于第一次运行,但当它返回到while循环的顶部时,它打印出“请输入人物的类别:”,然后直接跳到默认语句“请输入此人的姓名:“没有让用户有机会再次输入此类型的人。我最初用if语句(编辑出来)做了这个,但尝试将它切换到switch语句,以防万一可以解决它。我显然还有其他三个班级,但这可能是浪费时间,因为我很确定这个课程存在问题。