我写了一些Java代码,如下所示,但它不像我预期的那样运行。靠近底部,从if (upgrade == "Y")
行开始。我做了一个测试,我输入了Y
,但是这行并没有执行。你能帮我弄清楚为什么会发生这种行为吗?Java代码没有按预期执行
import java.io.*;
class P4
{
public static int get_price(String day_of_week, String age_group)
{
int price=0;
if (day_of_week == "WD")
{
if (age_group == "adult")
price = 66;
else if (age_group == "child")
price=48;
else
price = 32;
}
else
{
if (age_group == "adult")
price = 72;
else if (age_group == "child")
price=52;
else
price = 36;
}
return price;
}
public static void main(String[] args)
{
String adult2=null;
String child2=null;
String senior2=null;
String day_of_week=null;
String upgrade=null;
System.out.println("Enter Number of Adult Ticket:");
BufferedReader adult1 = new BufferedReader(new InputStreamReader(System.in));
try
{
adult2 = adult1.readLine();
}
catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
System.out.println("Enter Number of Child Ticket:");
BufferedReader child1 = new BufferedReader(new InputStreamReader(System.in));
try
{
child2 = child1.readLine();
}
catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
System.out.println("Enter Number of Senior Ticket:");
BufferedReader senior1 = new BufferedReader(new InputStreamReader(System.in));
try
{
senior2 = senior1.readLine();
}
catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
System.out.println("Choose Weekday or Weekend Pass (WD/WE):");
BufferedReader day_of_week1 = new BufferedReader(new InputStreamReader(System.in));
try
{
day_of_week = day_of_week1.readLine();
}
catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
System.out.println("Upgrade to Express Pass (Y/N):");
BufferedReader upgrade1 = new BufferedReader(new InputStreamReader(System.in));
try
{
upgrade = upgrade1.readLine();
}
catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
int adult = Integer.parseInt(adult2);
int child = Integer.parseInt(child2);
int senior = Integer.parseInt(senior2);
int total_a = adult * get_price(day_of_week, "adult");
int total_c = child * get_price(day_of_week, "child");
int total_s = senior * get_price(day_of_week, "senior");
int total_price = total_a + total_c + total_s;
int total_people = adult + child + senior;
int upgrade_price = 0;
if (upgrade == "Y")
{
if (day_of_week == "WD")
{
upgrade_price = total_people * 30;
}
else
{
upgrade_price = total_people * 68;
}
}
else
upgrade_price = 0;
int price = upgrade_price + total_price;
System.out.println("The total price is $" + price);
}}
==仅用于比较引用在你的情况等于()将是正确的选择....你的IDE的调试功能(如果你有任何使用),将使你的生活更容易这些情况.....你不仅会了解你的程序的工作,而且还会减少解决错误所需的时间...... – aProgrammer
我真的,真的推荐Joshua Block:用于学习java的“Effective Java”成语如何如何比较对象。 HTTP://java.sun。com/docs/books/effective/ –