Java代码未按预期执行

时间:2012-01-20 07:04:05

标签: java

我已经编写了一些Java代码,如下所示,但它的行为并不像我预期的那样。靠近底部,从行if (upgrade == "Y")开始。我做了一个测试,我输入Y但是这行没有执行。你能帮我解决一下这种行为的原因吗?

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);

}}

5 个答案:

答案 0 :(得分:7)

尝试使用.equal ..否则if(age_group.equals(“child”)

查看.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;
}

答案 1 :(得分:5)

您无法将字符串与==进行比较,您需要使用equals()

if("Y".equals(upgrade))

(我倾向于将常数放在第一位,以处理upgrade == null

的情况

答案 2 :(得分: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);

}}

答案 3 :(得分:2)

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

使用

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

而不是

upgrade == "Y"

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

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

答案 4 :(得分:0)

我注意到你的update变量是String,因此你应该使用:

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

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