代码没有错误但结果为false

时间:2013-10-26 12:41:43

标签: java if-statement

我写的程序..用户必须输入格式的字符串(条形码,价格:项目数) 我计算条形码总数

我的代码是

import java.util.*;

public class hanoofee2 {
    public static void main (String args[]) {
        Scanner input=new Scanner (System.in);

        System.out.println("Enter a string with format (Barcode,price:number of item) ");
        String ent = input.nextLine();
        String barcode = ent.substring(0,(ent.indexOf(",")));
        String price = ent.substring((ent.indexOf(","))+1,(ent.indexOf(":")));
        String number = ent.substring(ent.indexOf(":")+1,(ent.length()));

        double price2 = Double.parseDouble(price);
        int number2 = Integer.parseInt(number);
        double total = price2 * number2;

        String m = "";
        if(barcode == "11A") {
            m ="green pen";
        } else if(barcode == "22B") {
            m ="printer";
        } else if(barcode == "44C") {
            m ="Memory card";
        } else if(barcode == "44D") {
            m ="Cook book";
        }

        System.out.printf("The total price of%2s "+" is:%.2f ", m ,total);
    }
}

每件事都没问题 但是当我按下跑步时 “m”的值不显示

     Enter a string with format (Barcode,price:number of item) 
       22B,222:30
       The total price of    is:6660.00 
        ----jGRASP: operation complete.

有什么问题?!

1 个答案:

答案 0 :(得分:2)

  

每件事情都没问题但是当我按下跑步时,“m”的值不显示

请勿使用==来比较字符串的内容。

改为使用equals()

Why should I use equals() instead of == ?