2017-12-24 4 views
-1

사용자가 주 메뉴로 돌아 오는 방법 문자 "M"을 입력하십시오. 그것들을 메인 메뉴 페이지로 안내 할 것입니까?편지 "M"을 클릭하면 기본 메뉴로 돌아 오는 방법은 무엇입니까?

while (! inChoice.equalsIgnoreCase ("M"));을 (를) 사용해 보았습니다. 하지만 작동하지 않습니다. 어떤 생각이 잘못 됐나요?

공공 정적 무효 searchByCountry() {

int n, i, j, x; 

    n = (uniqueCountries.length)-1; 

    for(i=0; i<n; i++) { 

     for(j=i+1; j<n;) { 

      if(uniqueCountries[i]==uniqueCountries[j]) { 

       for(x=j; x<n; x++) { 

        uniqueCountries[x] = uniqueCountries[x+1]; 
       } 

       n--; 

      } 

      else { 

       j++; 
      } 
     } 
    } 

    String msg = ""; 
    msg = ""; 

    for(i=0; i<n; i++) { 

     String allUniqueCountries = uniqueCountries[i]; 

     msg += (i+1) + ". " + allUniqueCountries +" "+ "\n";  
    } 

문자열 입력 JOptionPane.showInputDialog = (null의, + MSG "를 국가 번호 \ 입력 없음");

int countryChoice = Integer.parseInt(input); 

    String result = ""; 

    int count = 1; 

    if(countryChoice == 1) { 



     for(i=0; i<3; i++) { 



     JOptionPane.showInputDialog(null, "Search result: " + count + "\n" 
     + "=======================\n" 
     + "Country: " + country[i] + "\n" 
     + "Month: " + travelmonth[i] + "\n" 
     + "Description: " + description[i] + "\n" 
     + "Price: $" + price[i] + "\n" + "=======================\n" 
     + "Enter M to return to main menu"); 
     count++;   

     } 

    } 

    //else if 
    if (countryChoice == 2) { 

     for(i=3; i<5; i++) { 

     JOptionPane.showInputDialog(null, "Search result: " + count + "\n" 
     + "=======================\n" 
     + "Country: " + country[i] + "\n" 
     + "Month: " + travelmonth[i] + "\n" 
     + "Description: " + description[i] + "\n" 
     + "Price: $" + price[i] + "\n" 
     + "=======================\n" 
     + "Enter M to return to main menu"); 
     count++; 

     } 

    } 

다른 경우 (countryChoice의 == 3) {

 for(i=5; i<8; i++) { 

     JOptionPane.showInputDialog(null, "Search result: " + count + "\n" 
     + "=======================\n" 
     + "Country: " + country[i] + "\n" 
     + "Month: " + travelmonth[i] + "\n" 
     + "Description: " + description[i] + "\n" 
     + "Price: $" + price[i] + "\n" 
     + "=======================\n" 
     + "Enter M to return to main menu"); 
     count++; 

     } 
    } 

    while(!inChoice.equalsIgnoreCase("M")); 
} 

} 당신은 실제로 새로운 입력을하고 "M"을 일치하면 확인해야합니다

+0

코드/콘텐츠를 추가하려면 게시물을 수정하십시오. – Zachary

+1

@Zachary 편집 됨 – Cherlyn

+0

inChoice는 어디에 있고 잠시 (...) 언급하지 않습니까? 게시 한 콘텐츠가 관련성이 있습니까? 사용자로부터 입력을 받으면 그것이 M인지 확인하는 것이 가장 좋은 방법입니다. – Zachary

답변

0

. 질문을 제시 한 방식으로 실제로 입력을 관리하는 방법을 알기 어렵습니다. 사용자 입력을 위해 Scanner을 사용합니다.

Scanner sc = new Scanner(System.in); 
while (true) { 
    String line = sc.nextLine(); 
    if (line.equalsIgnoreCase("M")) { 
     return; 
    } 
} 

또한, 이와 같은 기능적 방법으로 폴링하는 것은 바람직하지 않습니다. waitForM()과 같은 메서드를 호출 할 수 있습니다.이 메서드는 본체에서 searchByCountry() 메서드를 호출 한 후에 만 ​​수행됩니다. 이 같은 것이 훨씬 더 잘 처리 될 것입니다.

/* Scanner used to read input from console */ 
    public static Scanner sc = new Scanner(System.in); 

    /* Main entry point to program */ 
    public static void main (String args[]) throws Exception { 
     String input = ""; 
     /* Keep getting input until it is "exit", then finish */ 
     while (!(input = getInput()).equalsIgnoreCase("exit")) { 
      System.out.println("Enter desired input. Type exit to exit"); 
      if (input.equalsIgnoreCase("search")) { 
       SearchByCountry(); 
      } 
      WaitForM(); 
     } 
    } 

    public static void SearchByCountry() { 
     /* Do Stuff */ 
    } 

    /* Method to wait for M Input */ 
    public static void WaitForM() { 
     System.out.println("Enter M to return to Menu"); 
     String input = ""; 
     while (!(input = getInput()).equalsIgnoreCase("M")) { 
      return; 
     } 
    } 
+0

'sc'는 무엇입니까? – Cherlyn

+0

죄송합니다. 그것은 입력을 찾는 스캐너입니다. – Zachary

관련 문제