2017-12-07 6 views
0
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; 
    } 

} 

登録システムを作成しようとしています。今、私はプログラムを作って、学生が教職員やコースを追加するかどうかを選択するよう促します。ユーザーがオプションを選択して質問された質問に記入すると、プログラムは終了します。ユーザーが質問に答えると、最初のプロンプトに戻り、3つの選択肢が与えられます。switch文のループを作成する方法は?

+0

は、whileループを使用してください。パラメータとして 'true'を使うことができ、無期限にループします。ユーザ要求に応じて終了するには、ユーザ入力に基づいてパラメータをブール値に設定します(「終了しますか?(y/n)」のようなプロンプトが表示されます) – ChickenFeet

+0

改行ラベル。ちょうどgotoのように –

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

「しかし、それは本当に前に何が違うのではないのです」あなたが言うMIT - いいえ、それはですあなたのコードはメソッドを1つの場所から呼び出しているだけなので、そうではありません。良い部分は、コードのすべての行をもう一度繰り返さなくても、別の場所からそのメソッドを呼び出すことができ、学生で読むことができることです。また、私たちのプログラムを拡張して人や組織の町々を扱う時までには、1万行に及ぶ私たちの主力(方法でもあります)を停止します。メソッドが2つ以上のスクリーンを超えてはならないという規則に常に従ってください。それがある場合、それはあなたのコードが複数のメソッドに壊す必要が兆候だ、仕事の小さな単位自体

+0

これは私がもっと多くのことを理解するのに役立ちます、ありがとうございます。そのメソッドを別のクラスから呼び出す場合は、どうすればよいでしょうか? – liquidOfficer

+0

そのメソッドはpublicでなければならず、他のクラスはこのクラスのインスタンスへの参照を持つ必要があります –

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

完璧な例 –

関連する問題