0

我写了程序..用户必须输入一个格式为(条形码,价格:商品数量)的字符串,然后我计算条形码的总数

我的代码是

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.

问题是什么 ?!

4

1 回答 1

2

一切正常,但是当我按下运行时,“m”的值不显示

不要==用来比较字符串的内容。

改为使用equals()

为什么我应该使用 equals() 而不是 == ?

于 2013-10-26T12:42:44.287 回答