为什么我的java程序会跳过if语句?

时间:2013-11-03 17:24:40

标签: java if-statement skip

我只有15和java的新手所以我正在尝试构建一个简单的计算器,但我似乎无法弄清楚为什么这个if语句被忽略。我检查确保所有值都被存储,是的,所以我看不到任何其他问题可以解释这一点。任何帮助都会很棒!在第二个类//这个if语句

中查找注释

第一堂课

public class CalculatorOperations {

    double fnum, snum,answer;
    String operation;

    void plus(){

        operation="+";
        answer = fnum + snum;
    }

    void subtract(){

        operation="-";
        answer = fnum - snum;
    }

    void multiple(){

        operation="*";
        answer = fnum * snum;
    }

    void divide(){

        operation="/";
        answer = fnum / snum;
    }

    void invalidOperation(){

        System.out.println("Invalid operation.");
    }

    void showAttributes(){
        System.out.println(fnum);
        System.out.println(snum);
        System.out.println(operation);
    }
}

第二课

    import java.util.Scanner;

public class calculatorApplication {

    public static void main(String [] args){

        CalculatorOperations Operators = new CalculatorOperations();
        Scanner userInput = new Scanner(System.in);

        String loop2 = null;
        boolean loop;

         while (loop = true){

            // Getting input and storing it
                System.out.print("Please enter first number: ");
                Operators.fnum = userInput.nextDouble();

                System.out.println("TEST:"+Operators.fnum);

                System.out.print("Please enter second number: ");
                Operators.snum = userInput.nextDouble();

                System.out.println("TEST:"+Operators.snum);

                System.out.print("Please enter operation (+, -, * or /): ");
                Operators.operation = userInput.next();

                System.out.println("TEST:"+Operators.operation);

                // this if statement

                if (Operators.operation == "+") {
                    Operators.plus();

                } else if (Operators.operation == "-") {
                    Operators.subtract();

                } else if (Operators.operation == "*") {
                    Operators.multiple();

                } else if (Operators.operation == "/") {
                    Operators.divide();

                } else {

                    Operators.invalidOperation();
                }



                System.out.println("Answer: " +Operators.answer);

                System.out.print("Would you like to do another sum? (yes or no): ");
                loop2 = userInput.next();


         }

         if (loop2.equals("yes") || loop2.equals("Yes")){
             loop = true;
                System.out.println();
                System.out.println();
            }else{
                loop = false;
                  // Closes scanner to prevent resource leaks 
                userInput.close();
                System.exit(0);
            }


                }

    }

3 个答案:

答案 0 :(得分:2)

将字符串与==进行比较通常不会按照您希望的方式运行。这是因为字符串是对象,==将对象引用相互比较,而不是检查字符串是否包含相同的文本。

请尝试String.equals

if (Operators.operation.equals("+")) {
... //and of course the same for the rest of the statements

祝你的计划好运!

答案 1 :(得分:0)

使用.equals(String)方法,而不是==。您的if结构将改为:

if(Operators.operation.equals(“+”)){         Operators.plus();

} else if (Operators.operation.equals("-")) {
    Operators.subtract();

} else if (Operators.operation.equals("*")) {
    Operators.multiple();

} else if (Operators.operation.equals("/")) {
    Operators.divide();

} else {
    Operators.invalidOperation();
}

.equals(String)用于比较字符串,而==用于比较其他所有字符串。 ==正在比较对象的引用,.equals(String)用于比较String

另外,将while (loop = true)更改为while(loop)while (loop == true);否则你表明你实际上正在改变loop的价值。

答案 2 :(得分:0)

您不希望将字符串与 == 进行比较,因为这样做是为了比较字符串的引用,而不是字符串的值。您需要使用 .equals 方法。

if (Operators.operation.equals("+"))

来自 javadoc

  

boolean equals(Object anObject)

     

当且仅当参数是一个String对象,表示与此对象相同的字符序列时,返回true。

此外,您需要更改

while (loop = true)

while (loop)

= 是赋值运算符, == 是比较运算符。