简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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