當我輸入字符串'決定'的值爲'是'時,if語句就像通過執行System.out.println行的行爲就好像決定的值不是'是'一樣(「請繼續購物」);我能做些什麼來解決這個問題?下面是類的問題是:Java嘗試捕獲返回虛假變量
//paying for this instance of a basket
public void payBasket(double cash){
String decision;
double balance;
System.out.println("You have £" +cash);
decision = null;
if(cash > endTotal)
{System.out.println("You have enough money to pay for this basket");
}
else
{System.out.println("You need more money before you can pay for these items");};
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Would you like to pay for this basket?");
try{
decision = reader.readLine();
}
catch (IOException ioe){
System.out.println("an unecpected error");
}
if(decision == "Yes"){balance = endTotal - cash;
System.out.println("Your current balance is " + balance);}
else {System.out.println("Please continue shopping");
}
}
使用.equals()比較字符串..! – BlackPOP
http://stackoverflow.com/questions/20596624/cant-get-java-to-assign-a-value-to-a-variable-using-if-statement –