2013-12-11 1 views
0

이 루프가 호출되면 무한대로 실행되고 사용자가 아무 것도 입력하지 않고 catch 오류를 표시하는 경우. 나는 이것에 대한 어떤 이유도 찾을 수 없다. 제안?루프가 무한 반복되고 사용자에게 묻지 않고 오류 메시지 표시

public Purchase groceryStoreMenu(LemonadeStand lemonadeStand) { 

    boolean getMenu = true; 
    int userEnteredNumber = -1; 
    currentPurchase = new Purchase(); 

    while(getMenu){ 
     try{ 

      System.out.println("Grocery Store"); 
      System.out.printf("%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n" , "1:" , "Buy lemons", "2:", "Buy cups" , "3:" , "Buy sugar" , 
      "4:" , "Buy ice" , "5:" , "Done"); 

      userEnteredNumber = reader.nextInt(); 

      if (userEnteredNumber == 1) { 
       money = lemonadeStand.profit(0); 
       lemonsMenu(money); 
      }else if (userEnteredNumber == 2){ 
       money = lemonadeStand.profit(0); 
       cupsMenu(money); 
      }else if (userEnteredNumber == 3){ 
       money = lemonadeStand.profit(0); 
       sugarMenu(money); 
      }else if (userEnteredNumber == 4){ 
       money = lemonadeStand.profit(0); 
       iceMenu(money); 
      }else if (userEnteredNumber == 5){ 
      getMenu = false; 
      } else { 
      throw new Exception(); 
      } 
      } catch(Exception e) { 
      System.out.println("Error in number format. Enter a valid number from the choices (1,2,3,4,5)"); 
      } 

    } 
    return currentPurchase; 
+0

컴파일 시간 오류가 발생하지 않았습니까? –

+0

@VD '아니에요. – user3053348

+0

독자가 스캐너라고 가정합니다. 이것 좀 봐 : http://stackoverflow.com/questions/11643222/java-scanner-nextint –

답변

0

코드가 reader.nextInt()에 멈추지 않습니다. 이것은 아마도 해당 메소드에서 사용자 입력을 기다리지 않기 때문일 수 있습니다.

+0

그걸 고쳐 줘! 내가해야만하는 것은 광고였다. getMenu = false 정말 고마워. – user3053348

+0

신난다, 환영합니다! – jbangerter

+0

그럴만 한 답을 친절하게 받아 들여주십시오. – jbangerter

0

당신은

System.out.println("Grocery Store"); 
      System.out.printf("%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n" , "1:" , "Buy lemons", "2:", "Buy cups" , "3:" , "Buy sugar" , 
      "4:" , "Buy ice" , "5:" , "Done"); 
      Scanner reader = new Scanner(System.in); 
      userEnteredNumber = reader.nextInt(); 
다음 시도 사용자 입력을 읽기 위해 스캐너를 정의 할 필요가
0
System.out.printf("%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n" , "1:" , "Buy lemons", "2:", "Buy cups" , "3:" , "Buy sugar" , 4:" , "Buy ice" , "5:" , "Done"); 

"%의 \ t %의 %의 6 개 세트가 있습니다

문자열 형식의 실종 매개 변수가 있습니다 n "옵션에 5 세트 만 필요하면 6 번째 누락 매개 변수가 예외를 생성합니다

+0

고맙습니다. 그것도 내 다른 문제를 해결합니다. 처음에이 메뉴를 메인 메뉴에서 호출했지만 자동으로 나중 메뉴로 이동했습니다. – user3053348