我寫了一些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/ –