繁体   English   中英

如何使switch语句循环?

[英]How to make a switch statement loop?

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;
    }

}

我正在尝试建立一个注册系统。 现在,我已经编写了程序,以便它提示用户选择是否要添加学生,教职员工或课程。 一旦用户选择了一个选项并填写了所提出的问题,程序便会结束。 我该如何使其循环播放,以便用户回答问题后,将其带回到第一个提示,在其中给他们提供他们想要做的三种选择的选择?

在整个交换机周围放一个环,例如:

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;

你不得不说:“但是它与以前没有什么真正的不同。”-没什么,因为您的代码只能从一个地方调用该方法。 好的地方是,您可以从另一个地方调用该方法以读入学生,而不必再次重复所有代码行。 到我们将程序扩展到处理各种类型的人员和组织的整个城镇时,它也使我们的主要(也是一种方法)结束了上万行。 尝试始终遵循一个规则,即方法的长度不应超过几个屏幕。 如果是这样,则表明您的代码需要分解为更多的方法,更小的工作单元

你(们)能做到

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM