2012-01-20 259 views
-1

我写了一些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); 

}} 
+2

==仅用于比较引用在你的情况等于()将是正确的选择....你的IDE的调试功能(如果你有任何使用),将使你的生活更容易这些情况.....你不仅会了解你的程序的工作,而且还会减少解决错误所需的时间...... – aProgrammer

+0

我真的,真的推荐Joshua Block:用于学习java的“Effective Java”成语如何如何比较对象。 HTTP://java.sun。com/docs/books/effective/ –

回答

7

尝试使用.equal ..所以,如果其他(age_group.equals( “子”)

时退房.equals()方法:http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#equals(java.lang.Object)

和。 equalsIgnoreCase()方法: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)

import java.io.*; 

class P4 
{ 
    public static int get_price(String day_of_week, String age_group) 
    { 
    int price=0; 

    if (day_of_week.equals("WD")) 
    { 
     if (age_group.equals("adult")) 
     price = 66; 
     else if (age_group.equals("child")) 
     price=48; 
     else 
     price = 32; 
    } 
    else 
    { 
     if (age_group.equals("adult")) 
     price = 72; 
     else if (age_group.equals("child")) 
     price=52; 
     else 
     price = 36; 
    } 

    return price; 
} 
2

对于字符串中的平等条件,您需要equal方法。

,而不是使用

"Y".equals(upgrade); //is a good idea 

upgrade == "Y" 

因为字符串是对象,如果两个字符串具有相同的对象的equals(Object)方法将返回true。 ==运算符只有在两个String引用指向相同的底层String对象时才会成立。因此,当通过equals(Object)方法进行测试时,表示相同内容的两个字符串将相等,但如果它们实际上是同一个对象,那么在使用==运算符进行测试时只会相等。

参考http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

+0

我们必须首先为字符串比较 例如 –

+0

好吧......但在我的公共静态诠释get_price()如果day_of_week ==“WD”的作品。任何想法为什么? –

+0

尝试升级==“是”它不会工作==只有当两个字符串引用指向相同的基础字符串对象时,==运算符才为真 –

5

你不能比较==字符串,你需要使用equals()

if("Y".equals(upgrade)) 

(我倾向于把恒定的第一,处理情况upgrade == null

+0

+1 StringLiteral.equals(variable) – Jeffrey

4

java string comparison

String s = "something", t = "maybe something else"; 
    if (s == t)  // Legal, but usually WRONG. 
    if (s.equals(t)) // RIGHT 
    if (s > t) // ILLEGAL 
    if (s.compareTo(t) > 0) // CORRECT> 

所以你的情况,使用:

if(upgrade.equals("Y")) { 
     //your codes 
} 

import java.io.*; 

class P4 
{ 
public static int get_price(String day_of_week, String age_group) 
{ 
    int price=0; 

    if (day_of_week.equals("WD")) 
    { 
     if (age_group.equals("adult")) 
      price = 66; 

     else if (age_group.equals("child")) 
      price=48; 

     else 
      price = 32; 
    } 
    else 
    { 
     if (age_group.equals("adult")) 
      price = 72; 

     else if (age_group.equals("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.equals("Y")) 
    { 
     if (day_of_week.equals("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); 

}} 
0

我notised你update变量是字符串,因此,你应该使用:

if (upgrade.compareTo("Y") == 0) { 
     //your code 
    } 

这是比较字符串的正确方法。

+0

我刚刚测试了您的代码上面和它的工作。您也可以使用upgrade.compareToIgnoreCase(“Y”)== 0 – karla