简体   繁体   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;
    }

}

I am trying to make an enrollment system. 我正在尝试建立一个注册系统。 Right now I have made the program so that it prompts the user to choose if they want to add a student, faculty, or course. 现在,我已经编写了程序,以便它提示用户选择是否要添加学生,教职员工或课程。 Once the user picks an option and fills out the questions asked, the program ends. 一旦用户选择了一个选项并填写了所提出的问题,程序便会结束。 How can I make it loop so that after the user answers the questions, it brings them back to the first prompt where they are given the three choices of what they want to do? 我该如何使其循环播放,以便用户回答问题后,将其带回到第一个提示,在其中给他们提供他们想要做的三种选择的选择?

Put a loop around the entire switch, for example: 在整个交换机周围放一个环,例如:

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

I'd also break your code up a bit with methods. 我还将使用方法将您的代码分解。 If code is like a recipe, methods are small parts of the process that always perform the same, though might have different inputs - for example, you can beat an egg, beat cream, stir soup, stir sauce, stir flour - methods are typically named with something that sounds like an action (a verb) so if you hired a new young chef, who'd never seen cream before but knew how to beat things, you could hand him your cream and your eggs and tell him to beat them.. that's the idea of a method, that you take a small part of code that always does the same thing, though the inputs and outputs change, and make it a method. 如果代码就像一个配方,那么方法只是过程中的一小部分,尽管可能有不同的输入,但它们总是执行相同的操作-例如,您可以打鸡蛋,打奶油,搅拌汤,搅拌酱汁,搅拌面粉-通常是方法以听起来像动作(动词)的名字来命名,因此,如果您雇用了一位新厨师,因为他以前从没看过奶油,但知道如何打浆,可以将奶油和鸡蛋递给他,告诉他打浆..这就是方法的思想,尽管输入和输出发生了变化,但是您花了一小部分总是做同样事情的代码,并使其成为一种方法。 Input.nextLine() is a method of scanner. Input.nextLine()是扫描器的一种方法。 It always reads from the console (when the console is attached to the scanner) and gives a line of text, whatever text was typed. 它总是从控制台读取(将控制台连接到扫描仪时),并给出一行文本,无论键入什么文本。

Here's an example from your code: 这是您的代码示例:

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

That's a Method. 那是一种方法。 It takes input parameters, does something with them, gives something back 它需要输入参数,对它们进行处理,然后返回

You can now tidy up your switch statement to: 现在,您可以整理switch语句以:

   case 1:

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

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

"But it's not really any different to before" you mit say - no it's not, because your code only ever calls the method from one place. 你不得不说:“但是它与以前没有什么真正的不同。”-没什么,因为您的代码只能从一个地方调用该方法。 The good part is, you could call that method from another place, to read in a student, without have to repeat all the lines of code again. 好的地方是,您可以从另一个地方调用该方法以读入学生,而不必再次重复所有代码行。 It also stops our main (which is also a method) ending up ten thousand lines long by the time we've extended our program to handle entire towns of kinds of people and organisations. 到我们将程序扩展到处理各种类型的人员和组织的整个城镇时,它也使我们的主要(也是一种方法)结束了上万行。 Try to always follow a rule that a method shouldn't be more than a couple of screens long. 尝试始终遵循一个规则,即方法的长度不应超过几个屏幕。 If it is, it's an indication your code needs breaking up into more methods, smaller units of work per se 如果是这样,则表明您的代码需要分解为更多的方法,更小的工作单元

you can make 你(们)能做到

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