2013-02-26 3 views
1

// 다음 프로그램에서 처음에는 사용자가 지침을 환영 할 때 간단히 while 루프를 종료하기 위해 센티널을 입력 할 수 있습니다. 그러나 원하는 티켓 결과를 얻을 때 프로그램을 종료하지 않는 999을 누르면 구매하는 사용자가 999 브레이크 루프를 입력 한 후이 작업을 수행하지 않는루프 동안 센티넬 값이 종료되지 않습니다

import java.io.*; 

public class ManyTickets 
{ 

    public static void main (String [] args) throws IOException 
    { 
    String userInput; 
    String userInput2; 
    int intInput = 0; 
    int intInput2 = 0; 
    double total = 0.00; 
    BufferedReader ageInput = new BufferedReader (new InputStreamReader (System.in)); 
    try{ 
    System.out.println("Please enter your age, or press '999' to exit."); 
    userInput = ageInput.readLine(); 
    intInput = Integer.parseInt (userInput); 
    while (intInput != 999) 
    { 
     if (intInput < 0 || intInput > 110) 
     System.out.println("Invalid entry, or your age seems a bit too high to enter buy these tickets"); 
     else if (intInput <= 12) 
     { 
     total = total + 6; 
     System.out.println("The ticket cost for 1 ticket is " + total); 
     } 
     else if (intInput <= 64) 
     { 
     total = total + 11; 
     System.out.println("The ticket cost for 1 ticket is " + total); 
     } 
     else 
     { 
     total = total + 8; 
     System.out.println("The ticket cost for 1 ticket is $" + total); 
     } 
     System.out.println("So far, your tickets cost is: $" + total ); 
     System.out.print("Would you like to buy more tickets? You can buy up to 1 more ticket per customer! If no press 999 to exit"); 
     userInput = ageInput.readLine(); 
     intInput2 = Integer.parseInt (userInput); 
     } 
    }catch (NumberFormatException e){ 
     System.out.println("Please restart the program, and enter an integer instead!"); 
    } 
    } 
    { 
    double total = 0.0; 
    System.out.println("Thank you, The total cost for the ticket is: $" + total); 
    System.out.println("Have a nice day!"); 
    } 
} 

답변

2

때 두 개의 다른 변수를 사용하고 그 999 아래,

intInput2 = Integer.parseInt(userInput); 

if (intInput2 == 999) { 
break; 
} 
3

, intInputintInput2. 나는 두 번째 것이 필요한 이유를 알지 못한다 (catch 블록 바로 앞에). while 루프의 센티널 값과 비교하여 검사되는 하나 인 intInput을 다시 사용하십시오.

+0

이전 답변은 이미 효과가 있었지만 답변에 추가하려면 게시하기 전에 이미 시도했지만, 그 일은 센티널 가치입니다.하지만 계속하기 위해 키를 누를 때마다 계산이 수행되지 않는 문제가있었습니다. 12 세 이상 64 세 이하의 사람들에게는 64 세 이상이 추가 될 때마다 출력에 매번 6 달러가 추가됩니다. 8 달러 또는 11 세가됩니다. – user2109589

관련 문제