如何进行switch语句循环?

时间:2017-12-07 04:37:36

标签: java

import java.util.LinkedList;
import java.util.Scanner;

public class Enrollment {
public static void main(String[] args) {
    LinkedList<Student> studentData = new LinkedList<Student>();
    LinkedList<Faculty> facultyData = new LinkedList<Faculty>();
    LinkedList<Course> courseData = new LinkedList<Course>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter 1 to add a student, 2 to add a faculty, 3 to add a course");
    int userChoice = input.nextInt();
    input.nextLine();
    switch (userChoice) {

    case 1:
        System.out.println("Enter student full name ");
        String sName = input.nextLine();

        System.out.println("Enter student age ");
        int sAge = input.nextInt();

        System.out.println("Enter student id ");
        int sID = input.nextInt();
        input.nextLine();

        System.out.println("Enter student address(only address number and street name) ");
        String sAddress = input.nextLine();

        System.out.println("Enter city, state, and zip code ");
        String sCityStateZip = input.nextLine();

        System.out.println("Enter student gender ");
        String sGender = input.nextLine();

        Student studentInfo = new Student(sName, sAge, sID, sAddress, sCityStateZip, sGender);
        studentData.add(studentInfo);

        for (Student testClass : studentData) {
            System.out.println(testClass);
        }

        break;
    case 2:
        // code to add faculty
        System.out.println("Enter faculty name ");
        String fName = input.nextLine();

        System.out.println("Enter faculty age ");
        int fAge = input.nextInt();

        System.out.println("Enter faculty id ");
        int fID = input.nextInt();
        input.nextLine();

        System.out.println("Enter faculty degree ");
        String fDegree = input.nextLine();

        System.out.println("Enter faculty major ");
        String fMajor = input.nextLine();

        System.out.println("Enter faculty address ");
        String fAddress = input.nextLine();

        System.out.println("Enter faculty gender ");
        String fGender = input.nextLine();

        Faculty facultyInfo = new Faculty(fName, fAge, fID, fDegree, fMajor, fAddress, fGender);
        facultyData.add(facultyInfo);

        for (Faculty testClass : facultyData) {
            System.out.println(testClass);
        }
        break;
    case 3:
        // code to add course
        System.out.println("Enter course name ");
        String courseName = input.nextLine();

        System.out.println("Enter course ID ");
        int cID = input.nextInt();

        System.out.println("Enter number of credits ");
        int numOfCred = input.nextInt();

        System.out.println("Enter intstructor/faculty ID ");
        int instructorID = input.nextInt();

        System.out.println("Enter course year ");
        int year = input.nextInt();
        input.nextLine();

        System.out.println("Enter semester ");
        String semester = input.nextLine();

        System.out.println("Enter classroom size ");
        int size = input.nextInt();

        System.out.println("Enter course capacity ");
        int capacity = input.nextInt();

        Course courseInfo = new Course(courseName, cID, numOfCred, instructorID, year, semester, size, capacity);
        courseData.add(courseInfo);

        for (Course testClass : courseData) {
            System.out.println(testClass);
        }

        break;

    default:
        System.out.println("Invalid entry");
        break;
    }

}

我正在尝试建立一个注册系统。现在我已经制作了该程序,以便提示用户选择是否要添加学生,教师或课程。一旦用户选择了一个选项并填写了问题,程序就会结束。如何让它循环,以便在用户回答问题后,它会将它们带回第一个提示,在那里给出他们想要做的三个选择?

2 个答案:

答案 0 :(得分:1)

在整个开关周围放置一个循环,例如:

while(true){
  //entire switch statement here
}

我还会用方法稍微破坏你的代码。如果代码就像一个食谱,方法是过程的一小部分,总是执行相同的,虽然可能有不同的输入 - 例如,你可以击败鸡蛋,打奶油,搅拌汤,搅拌酱,搅拌面粉 - 方法通常用一种听起来像动作(动词)的东西来命名所以,如果你聘请了一位新的年轻厨师,以前从未见过奶油但知道如何打败东西,你可以把他的奶油和鸡蛋交给他并告诉他打败他们..这就是一个方法的想法,即尽管输入和输出发生了变化,你仍然会采用一小部分始终做同样事情的代码,并使其成为一种方法。 Input.nextLine()是一种扫描方法。它始终从控制台读取(当控制台连接到扫描仪时),并提供一行文本,无论输入的是什么文本。

以下是您的代码中的示例:

private Student getStudentFromInput(Scanner input){
        System.out.println("Enter student full name ");
        String sName = input.nextLine();

        System.out.println("Enter student age ");
        int sAge = input.nextInt();

        System.out.println("Enter student id ");
        int sID = input.nextInt();
        input.nextLine();

      //  ... blah blah and so on right the way to the bit where you make the student
        Student studentInfo = new Student(sName, sAge, sID, sAddress, sCityStateZip, sGender);

        return studentInfo;
}

这是一种方法。它需要输入参数,对它们做一些事情,给予回馈

您现在可以将switch语句整理为:

   case 1:

        Student studentInfo = getStudentFromInput(input);
        studentData.add(studentInfo);

        for (Student testClass : studentData) {
            System.out.println(testClass);
        }
        break;

“但它之前并没有什么不同”你说 - 不,不是,因为你的代码只从一个地方调用这个方法。好的部分是,您可以从其他地方调用该方法,以便在学生中阅读,而无需再次重复所有代码行。当我们扩展我们的程序以处理整个城镇的各种人和组织时,它也会阻止我们的主要(这也是一种方法)结束了一万行。尽量遵循规则,即方法不应超过几个屏幕。如果是,那就表明你的代码需要分解成更多的方法,更小的工作单位本身

答案 1 :(得分:1)

你可以制作

while(userChoice!=4){.........your code.........
 System.out.println("Enter 1 to add a student, 2 to add a faculty, 3 to add a course");
  userChoice = input.nextInt();
}