Java 如何将在if语句中设置的变量带入代码的其余部分?
在我打印出目的地和酒店的底部,它表示本地变量没有初始化。如何获取代码,以便在if语句中分配给这些变量的值一直保留到底部?初始化变量。例如,改变Java 如何将在if语句中设置的变量带入代码的其余部分?,java,variables,if-statement,Java,Variables,If Statement,在我打印出目的地和酒店的底部,它表示本地变量没有初始化。如何获取代码,以便在if语句中分配给这些变量的值一直保留到底部?初始化变量。例如,改变 import java.util.Scanner; public class TripCost { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String destination; String
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语句时,将获得正确的结果值,您可以声明String hotel=“”;例如希望能帮忙非常感谢!这似乎是一个重要的警告,我们的讲师应该提到这一点,您认为自己很幸运,因为您使用的是Java,编译器指出了这个编程错误;例如,如果你使用C++,你会发现,直到程序开始崩溃,因为神秘的原因!是的,感谢上帝让我们日食。我不能说我犯了多少次愚蠢的语法错误,eclipse说“嘿,不要那样做”很抱歉最初没有标记正确,我对这个网站不熟悉。我帮你纠正了!
String destination;
String destination = null;