Java try catch返回假变量
当我将字符串'decision'的值输入为'Yes'时,if语句通过执行line System.out.println(“请继续购物”),就好像decision的值不是'Yes';我能做些什么来解决这个问题?以下是问题所在的班级:Java try catch返回假变量,java,variables,if-statement,try-catch,Java,Variables,If Statement,Try Catch,当我将字符串'decision'的值输入为'Yes'时,if语句通过执行line System.out.println(“请继续购物”),就好像decision的值不是'Yes';我能做些什么来解决这个问题?以下是问题所在的班级: //paying for this instance of a basket public void payBasket(double cash){ String decision; double balance; System.out.pr
//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(String myValue)
方法,而不是=
在java中使用.equals()方法而不是“==”运算符来比较对象
改变
if(decision == "Yes")
到
如果要忽略大小写,请使用.equalsIgnoreCase()方法
这里
->if(decision==“Yes”)
使用
使用.equals()比较字符串。。!
if("Yes".equals(decision))
if("Yes".equalsIgnoreCase(decision))
if(descision.equals("Yes"));