2014-02-25 1 views
0

질문이 약간 오도 된 것 같아서 설명이 있습니다. - 메뉴를 만들고, 선택 사항을 나열하고, 입력을 요청하고, 사용자의 입력을 읽는 등의 방법을 알고 있습니다.Java에서 기본 메뉴를 만드는 방법은 무엇입니까?

실행은 순전히 논리적입니다. 나는 3 개의 선택으로 메인 메뉴를 만들어야합니다

-Create 관련 속성

- 검색 목록

-exit

학생 "객체"의 배열.

이 시점에서 나는 "하위 메뉴"를 활성화하는 루프 동안 부울 연산을했습니다. 불리언 값은 목록이나 검색 등을 만들지 여부에 따라 true 또는 false가됩니다.

(중요 사항 : 위의 클래스 (학생)은 배열의 모든 특성을 포함하고 있습니다. 다음과 같이 이름, 등급 평균 등)로 내 주요 프로그램의 일반적인 레이아웃은 다음과 같습니다

while(bMainMenu) 
     { 
      boolean bList = false; 
      boolean bSearch = false; 

      [all the code prompting choices] 
       -if input is "create" then bList = true; 
       -if input is "search" then bSearch = true; 
       -if input is "exit" then bMainMenu = false; 

      while(bList) 
       { 
        [all the code that creates the array and prompts for student info 
        where the array is as long as the user chooses] 
        bList = false; 
       } 

      while (bSearch) 
       { 
        [all the code for searching the array] 
        bSearch = false; 
       } 

     } 

목록을 생성 한 후, 다른 모든 거짓이며, 메인 메뉴를-루프 다시. 이번에는 사용자가 "검색"이라고 말하면 검색 메뉴에 대한 부울을 true로 만듭니다.

방금 ​​만든 배열을 참조하는 문제입니다. 나는 그것을 어디에서 불러야 하는지를 알아 내려고하고있다. 내가 할 수있는 한, 모든 것을 끝낸 방법은 배열을 "bList 루프"내에 포함시킵니다.

배열을 "bSearch"루프에 "보이게"만들려면 어디에서 호출해야합니까? 아니면 모든 것을 다르게 재구성해야합니까?

미리 감사드립니다.

+2

어레이는 필요한 블록이 액세스 할 수 있도록 범위 내에 있어야합니다. 블록이 잠시 동안 있으면 해당 블록에서만 볼 수 있습니다. –

답변

1
bMainMenu = true; 
List<StudentInfo> students = new ArrayList<StudentInfo>(); 
while(bMainMenu){ 

    boolean bList = false; 
    boolean bSearch = false; 

    [all the code prompting choices] 
       -if input is "create" then bList = true; 
       -if input is "search" then bSearch = true; 
       -if input is "exit" then bMainMenu = false; 

    while(bList){ 
     [all the code that creates the array and prompts for student info 
        where the array is as long as the user chooses] 

     // Build this student info object based on user parameters 
     StudentInfo studentInfo = new StudentInfo(name, class); 

     students.add(studentInfo); 
    } 

    while(bSearch){ 
     /*  
     Play with "students" arraylist. It is accessible here and contains all studentinfo details added before. 
     */ 
    } 
} 
1

이것은 모두 범위에 관한 것입니다. 어디서든 주요 외부 루프 내에서 부울 변수 bListbSearch에 액세스 할 수있는 방법과 유사 : while(bMainMenu), 당신이 유사하게 학생들의 배열을 선언 할 수

while(bMainMenu) { 
    Student[] students; 
    boolean bList = false; 
    boolean bSearch = false; 

    // Remaining code can now access students array as it is in scope 
} 

당신은 다음 나중에 배열의 인스턴스를, 예를 들면 : students = new Student[numberStudents]를 .

학생 수를 미리 알고 있거나 계속 커질 경우 모르는 경우 ArrayList<Student>을 고려하십시오.

+0

피 묻은 화려한. 감사! 나는 그것을 바로 인스턴스화하지 않고 배열을 선언하는 것에 대해 몰랐다 - 이것은 모든 것을 바꾼다! ArrayList 일을 고려했지만 교수님은 지금 당장이 방법을 사용하기를 원합니다. 그러면 나중에 학기에 ArrayList를 수행 할 것입니다. –

+0

실제로 내가 게시 한 방식으로 수행하는 문제는 변수를 초기화해야한다는 것입니다. 처음 while 루프가 될 때까지 초기화되지 않고 범위와 동일한 문제가 발생합니다. 제안하는 이클립스는 "Student [] students = Null;"이라고 말합니다. 하지만 분명히, 그건 잘못된 것입니다 ... –