Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java try catch返回假变量_Java_Variables_If Statement_Try Catch - Fatal编程技术网

Java try catch返回假变量

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

当我将字符串'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.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"));