2017-11-29 2 views
2

Ich versuche, Programm zu erstellen, das die Verwendung vieler Menüs erfordert (als Anfänger, beginnt dieses Zeug wirklich, mich zu überwältigen, aber es ist für ein Projekt für meine Klasse). Das ist, was ich habe, so weit:Kann ein Menü nicht richtig funktionieren, dass ich innerhalb einer verschachtelten Schleife erstellte

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

}

Insbesondere Ich habe Probleme in diesem Teil des Codes

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

Was geschieht, dass das Programm zunächst beginnt gut durch Präsentation des Benutzers mit der ersten von mir erstellten Methode "mainMenu". Wenn ich die Nummer 1 eingabe, um meine SubMenu-Methode zu öffnen, funktioniert das auch richtig. Wenn ich jedoch 1 erneut drücke (dieses Mal, um den Dienstplan anzuzeigen, der sich in der Datei befindet), wird das Untermenü erneut ausgedruckt. Ich drücke wieder 1 und dann zeigt es mir die Liste wie gewünscht. Ich kann mir nicht vorstellen, warum ich es beim ersten Mal nicht zeigen kann.

Antwort

1
int choice2 = subMenu(); 

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

     subMenu(); //calls subMenu method 

Sie rufen die subMenu() Methode zweimal, weshalb es zweimal ausgeführt wird.

+0

Hey @D M danke für den Hinweis! Das klärt mich so sehr auf, dass ich mich schäme, dass ich jetzt fragen muss, dass ich es besser verstehe, aber das ist Teil des Prozesses, denke ich. Ich habe eine andere Frage, wenn ich vom Untermenü zum Hauptmenü zurückkehren möchte, wie würden Sie mir raten, dass ich das tue? Springen von Menü zu Menü wird in meinem Programm unerlässlich sein. Wenn Sie etwas haben, über das ich etwas lesen kann, oder ein Video über so etwas, dann nehme ich das auch! Danke – Bryan

+0

Die Art, wie Sie es haben, ist nicht schlecht; Das Hauptmenü befindet sich in einer Schleife, die das Untermenü umgibt. –

Verwandte Themen