2017-11-29 7 views
2

多くのメニューを使用するプログラムを作成しようとしています(初心者の方には本当に私を圧倒し始めていますが、私のクラスのプロジェクトのために)。これは私がこれまで持っているものです。入れ子になったループ内で作成したメニューが正しく機能しない

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Scanner; 


public class GroupUs { 

public static void main(String[] args) { 


    String fileName = "CompConClass.txt"; //File includes class roster 

    System.out.println("Hello, would you like to access the class that you have on file or would you like to create a new class?"); 

    int choice = mainMenu(); 
    int choice2 = subMenu(); 

    while (choice != 0) { 
     if (choice == 1) { 

      subMenu(); //calls subMenu method 

     if (choice2 == 1) { 

      try { 
       BufferedReader br = new BufferedReader(new FileReader(fileName)); 

       String line = null; //create a line variable 
       while ((line = br.readLine()) != null) { //this will read the file line by line 

        System.out.println(line); //displays every line 
       } 

       } catch (FileNotFoundException ex) { 
        ex.printStackTrace(); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 


     } 


     } else if (choice == 2) { 
      System.out.println("test"); 
     } 

     choice = mainMenu(); 

     } 

    }   

    public static int mainMenu() { 
     Scanner scan = new Scanner(System.in); // Reading from System.in 
     System.out.println("Press 0 to quit.\n" 
       + "Press 1 to access the class that you have on file.\n" 
       + "Press 2 to create a new class.\n" 
       ); 
     return scan.nextInt(); // Scans the next token of the input as an int. 
    } 

    public static int subMenu() { 
     Scanner scan = new Scanner(System.in); // This method will give the teacher the ability to view the roster from the file or add students on to that file 
     System.out.println("What would you like to do to the class on file?\n" 
       + "Press 1 to view the students.\n" 
       + "Press 2 to add another student.\n" 
       + "Press 3 to remove a student." 
       ); 
     return scan.nextInt(); // Scans the next token of the input as an int. 
    }  

}

具体的には、私は何が起こっているコード

if (choice == 1) { 

     subMenu(); //calls subMenu method 

    if (choice2 == 1) { 

     try { 
      BufferedReader br = new BufferedReader(new FileReader(fileName)); 

      String line = null; //create a line variable 
      while ((line = br.readLine()) != null) { //this will read the file line by line 

       System.out.println(line); //displays every line 
      } 

      } catch (FileNotFoundException ex) { 
       ex.printStackTrace(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

のこの部分で問題を抱えていますが、プログラムが最初にすることにより、良好なオフを開始するということです私が作成した最初のmainMenuメソッドをユーザに提示します。サブメニューメソッドを開くために数字1を入力すると、正しく動作します。しかし、サブメニューの横に1をもう一度押すと(ファイルにある名簿を表示する)、サブメニューが再び表示されます。私はもう一度1を押してから、希望通りに私に名簿を表示します。なぜ私はそれを最初に表示することができないのか分かりません。

答えて

1
int choice2 = subMenu(); 

while (choice != 0) { 
    if (choice == 1) { 

     subMenu(); //calls subMenu method 

subMenu()メソッドを2回呼び出しているため、2回実行されています。

+0

ちょっと@D Mアドバイスありがとう!これは私のためにはっきりと分かりました。私はそれが私がそれをより良く理解していると尋ねなければならないと恥ずかしく思っていましたが、それは私が推測するプロセスの一部です。もう1つの質問があります。サブメニューからメインメニューに戻ってみたいと思ったら、どうやったらそれについてアドバイスしますか?私のプログラムでは、メニューからメニューへのジャンプが不可欠になるでしょう。あなたが読んでいるものや、このようなもののビデオがあれば、私もそれを取るでしょう!ありがとう – Bryan

+0

あなたが持っている方法は悪くないです。メインメニューはサブメニューを囲むループ内にあります。 –

関連する問題