2017-11-29 15 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 메서드를 사용자에게 표시합니다. 내 subMenu 메서드를 열려면 1을 입력 할 때 제대로 작동합니다. 그러나 하위 메뉴 옆에 1을 다시 누르면 (파일에있는 명단을 표시), 하위 메뉴가 다시 인쇄됩니다. 다시 1을 누른 다음 원하는대로 명단을 보여줍니다. 왜 처음으로 표시 할 수 없는지 알 수 없습니다.

답변

1
int choice2 = subMenu(); 

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

     subMenu(); //calls subMenu method 

subMenu() 메서드를 두 번 호출하므로 두 번 실행됩니다.

+0

안녕하세요 @D M 조언 주셔서 감사합니다! 이것은 나를 위해 너무 많은 것을 분명히합니다. 나는 부끄러움을 느끼지 만, 이제는 내가 더 잘 이해할 수는 있지만, 그것이 내가 생각하는 과정의 일부라고 부탁해야한다는 것을 느낍니다. 또 다른 질문이 있습니다. 하위 메뉴에서 메인 메뉴로 돌아가고 싶다면 어떻게해야할까요? 내 프로그램에서는 메뉴에서 메뉴로 점프하는 것이 필수적입니다. 내가 읽을 수있는 무언가를 가지고 있거나 이와 비슷한 비디오를 읽으면 나는 그것을 또한 가질 것이다! 감사합니다 – Bryan

+0

당신이 가지고있는 방식은 나쁘지 않습니다; 주 메뉴는 하위 메뉴를 둘러싼 루프에 있습니다. –