2014-10-07 24 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

非常感谢你!似乎是一个重要的警告,我们的教练应该提到大声笑 – 2014-10-07 20:31:47

+0

认为你很幸运,你使用的Java编译器指出这种编程错误;例如,如果你使用C++,直到你的程序因为神秘的原因而崩溃,你才会注意到! – 2014-10-07 20:32:50

+0

耶感谢上帝的日食。不能说有多少次我犯了一个愚蠢的语法错误和日食说:“嘿,不要这样做” – 2014-10-07 20:34:24

0

如果你的String变量没有被初始化将是一个空值,所以你需要初始化它的值,那么当变量经过你的if语句将得到正确的结果值时,你可以声明String hotel =“ “;例如。希望能帮到

相关问题