2014-10-07 3 views
0
import java.util.Scanner; 

public class TripCost 
{ 
    public static void main(String[] args) 
    { 
    Scanner keyboard = new Scanner(System.in); 
    String destination; 
    String hotel; 
    int days = 3; 
    double mealCost; 
    double regularMeal = 31.00; 
    double highCostMeal = 52.00; 
    double gasPrice = 3.29; 
    double miles; 
    double airfare; 
    double hotelCost; 
    double budgetel = 76.00; 
    double holidayInn = 109.00; 
    double sheraton = 138.00; 
    double hyatt = 215.00; 

    System.out.println("Here are the places you may go for vacation:"); 
    System.out.println("--------------------------------------------"); 
    System.out.println(" 1. Niagra Falls"); 
    System.out.println(" 2. Chicago"); 
    System.out.println(" 3. San Francisco"); 
    System.out.println(" 4. Las Vegas"); 
    System.out.println(" 5. St. Louis"); 
    System.out.println("--------------------------------------------"); 
    System.out.print("Please select the number of the destination: "); 
    int destinationChoice = keyboard.nextInt(); 


    if (destinationChoice == 1) 
    { 
     miles = 257.00; 
     airfare = 0; 
     mealCost = regularMeal; 
     destination = "Niagra Falls"; 
    } 
    else if (destinationChoice == 2) 
    { 
     miles = 303.00; 
     airfare = 0; 
     mealCost = highCostMeal; 
     destination = "Chicago"; 
    } 
    else if (destinationChoice == 3) 
    { 
     miles = 0; 
     airfare = 630.00; 
     mealCost = highCostMeal; 
     destination = "San Francisco"; 
    } 
    else if (destinationChoice == 4) 
    { 
     miles = 0; 
     airfare = 415.00; 
     mealCost = highCostMeal; 
     destination = "Las Vegas"; 
    } 
    else if (destinationChoice == 5) 
    { 
     mealCost = regularMeal; 
     destination = "St. Louis"; 
     System.out.print("Would you like to drive or fly? "); 
     String transportation = keyboard.next(); 
     if (transportation.equalsIgnoreCase("drive")) 
     { 
      miles = 551.00; 
      airfare = 0; 
     } 
     else if (transportation.equalsIgnoreCase("fly")) 
     { 
      miles = 0; 
      airfare = 290.00; 
     } 
     else 
     { 
      System.out.print("Invalid selection"); 
      System.exit(1); 
     } 
    } 
    else 
    { 
     System.out.print("Invalid destination"); 
     System.exit(1); 
    } 

    System.out.println("These are the hotels with vacancies:"); 
    System.out.println("------------------------------------"); 
    System.out.printf("%-15s%10s%.0f\n", "1. Budgetel", "$", budgetel); 
    System.out.printf("%-15s%10s%.0f\n", "2. Holiday Inn", "$", holidayInn); 
    System.out.printf("%-15s%10s%.0f\n", "3. Sheraton", "$", sheraton); 
    System.out.printf("%-15s%10s%.0f\n", "4. Hyatt", "$", hyatt); 
    System.out.println("------------------------------------"); 
    System.out.print("Which hotel would you like to stay at? "); 
    int hotelChoice = keyboard.nextInt(); 
    if (hotelChoice == 1) 
    { 
     hotelCost = budgetel; 
     hotel = "Budgetel"; 
    } 
    else if (hotelChoice == 2) 
    { 
     hotelCost = holidayInn; 
     hotel = "Holiday Inn"; 
    } 
    else if (hotelChoice == 3) 
    { 
     hotelCost = sheraton; 
     hotel = "Sheraton"; 
    } 
    else if (hotelChoice == 4) 
    { 
     hotelCost = hyatt; 
     hotel = "Hyatt"; 
    } 
    else 
    { 
     System.out.print("Invalid selection"); 
     System.exit(1); 
    } 

    System.out.println("Cost for your trip (drive to " + destination + ", staying at " + hotel + " for " + days + " days):"); 
    System.out.println("---------------------------------------------------"); 
    System.out.printf("%-13s%1s%3s%3.2f\n", "Transportation", ":", "$", miles/gasPrice); 
} 
} 

아래쪽에서 목적지와 호텔을 출력하기 위해 로컬 변수가 초기화되지 않는다고 말합니다. if 문에서이 변수에 할당 된 값이 맨 아래로 이어 지도록 내 코드를 어떻게 얻을 수 있습니까?if 문에서 설정 한 변수를 나머지 코드로 옮기는 방법은 무엇입니까?

답변

2

변수를 초기화하십시오. 예를 들어,

String destination; 

String destination = null; 

및 초기화되지 않은 모든 다른 변수들에 대해 동일한 변경.

+0

정말 고마워요! 우리 강사가 LOL을 언급해야만했던 중요한 경고처럼 보입니다. –

+0

컴파일러가이 프로그래밍 오류를 지적하는 곳에서 Java를 사용하고 있다는 점을 생각해보십시오. 예를 들어 C++을 사용한다면, 프로그램이 신비한 이유로 충돌하기 시작할 때까지주의를 기울일 필요가 없습니다! –

+0

그래, 일식에 대한 하나님 감사합니다. 위선자가 어리석은 문법 오류를 만들었고 "이봐,하지 마라"라고 말하는 말을 할 수 없다. –

0

String 변수가 초기화되지 않은 경우 값을 초기화해야하므로 변수가 if 문을 통과 할 때 결과에 ​​적합한 값을 얻을 수 있습니다. String hotel = " "; 예를 들면. 도와 드리겠습니다.

관련 문제