if循环中的if语句JAVA

时间:2016-04-21 02:49:46

标签: java

尝试做菜单选择,但不知道为什么它会工作。 当我完成添加一所学校时,我想回去菜单。 我如何退出并打印ARRAYLIST的所有信息?

  1. 打印菜单
  2. 用户选择创建小学,大学或高中生
  3. 提示用户提供适当的信息
  4. 创建适当的对象(调用构造函数)
  5. 将对象放入ArrayList学生
  6. 当用户从菜单中选择4.时,
  7. 打印出所有学生
  8. 重复直到用户从菜单中选择“退出”
  9.   System.out.println("Please select type of object to create:");
        System.out.println(
     "1) Elementry School\n2) High School\n3) College Student\n4)Print out all students\n5)Exit");
       int menuItem = Integer.parseInt(in.nextLine());
        while (menuItem != 5) {
            if (menuItem == 1) {
                System.out.println(
                        "You have selected to create a profile of a Elementary School Kid ..... ");
                System.out.println("Please enter the full name: ");
                String name = in.nextLine();
                System.out.println("Please enter the Age: ");
                int age = Integer.parseInt(in.nextLine());
                System.out.println(
                        "Please enter the name of the Elementry School: ");
                String eschool = in.nextLine();
                System.out.println("Please enter the City: ");
                String city = in.nextLine();
                System.out.println("Please enter the name of the Teacher: ");
                String tname = in.nextLine();
                System.out.println("Please enter the Room No.: ");
                int rno = Integer.parseInt(in.nextLine());
                students.add(new ElementarySchoolStudent(name, age, city, eschool,
                        tname, rno));
            }
    
            else if(menuItem==2){
                System.out.println(
                        "You have selected to create a profile of a High School student ..... ");
                System.out.println("Please enter the full name: ");
                String n = in.nextLine();
                System.out.println("Please enter the Age: ");
                int a = Integer.parseInt(in.nextLine());
                System.out.println(
                        "Please enter the name of the High school: ");
                String highsch = in.nextLine();
                System.out.println("Please enter the City: ");
                String add = in.nextLine();
                System.out.println("Please enter the counselor's name:");
                String counselor=in.nextLine();
                System.out.println("Please enter student’s gpa:");
                double g=in.nextDouble();
                System.out.println("Please enter the year of graduation:");
                int year=in.nextInt();
                students.add(new HighSchoolStudent(n,a,add,highsch,counselor,
                          year, g));
            }
    
            else if(menuItem==3){
                System.out.println(
                        "You have selected to create a profile of a college student ..... ");
                System.out.println("Please enter the full name: ");
                String n = in.nextLine();
                System.out.println("Please enter the Age: ");
                int a = Integer.parseInt(in.nextLine());
                System.out.println(
                        "Please enter the name of the College: ");
                String college = in.nextLine();
                System.out.println("Please enter the City: ");
                String add = in.nextLine();
                System.out.println("Please enter the major of the student:");
                String mjr=in.nextLine();
                System.out.println("Please enter student's gpa:");
                double g=in.nextDouble();
                students.add(new CollegeStudent(n,a,add,college,mjr,g));
            }
    
            else if(menuItem==4){
                for(Student current : students)
                System.out.println(current.printStudent());
            }
    
    
        }
    

    }

1 个答案:

答案 0 :(得分:1)

将您的菜单放入while循环中,以便每次输入如下选项时都会打印:

int menuItem = 0;
while (menuItem != 5) {
    System.out.println("Please select type of object to create:");
    System.out.println("1) Elementry School\n2) High School\n3) College Student\n4)Print out all students\n5)Exit");
    menuItem = Integer.parseInt(in.nextLine());
    if (menuItem == 1) {
        ...
    }
    ...
}