-2

这个程序应该是一个基本的计算器,它接受两个双输入,然后提示用户使用相应的数学运算符键入一个字符串。由于某种原因,在用户输入与菜单对应的字符串后,我的基本计算器不会返回值。

 import java.util.Scanner;  
    public class Assignment3 {  
        public static void main (String[] args)  
     {
        String operator = "";
        double userInputOne;
        double userInputTwo;
        double output = -.00001;
        String comparison = "";


         Scanner scan = new Scanner(System.in);

         System.out.println("Enter the first number of your calculation:");
         userInputOne = scan.nextDouble();

         System.out.println("Enter the second number of your calculation:");
         userInputTwo = scan.nextDouble();

          System.out.println("Type in one direction under Calculator Options:");
          System.out.println("Add, Subtract, Multiply, Divide, Mod");
          System.out.println("Compare");
          System.out.println("Print Numbers");
          System.out.println("Quit");
          Scanner scanTwo = new Scanner(System.in);
          operator = scanTwo.nextLine();

        if (operator == "Add")
        {
            output = userInputOne + userInputTwo;
        }else if (operator == "Subtract"){
            output = userInputOne - userInputTwo;
        }else if (operator == "Multiply"){
            output = userInputOne * userInputTwo;
        }else if (operator == "Divide"){
            output = userInputOne / userInputTwo;
        }else if (operator == "Mod"){
            output = userInputOne % userInputTwo;
        }else if (operator == "Compare"){
            if(userInputOne == userInputTwo){
                comparison = (userInputOne + " Is equal to " +      userInputTwo);
                System.out.println(comparison);
            }else if (userInputOne > userInputTwo){
                comparison = (userInputOne + " Is greater than " + userInputTwo);
                System.out.println(comparison);
            }else if (userInputOne < userInputTwo){
                comparison = (userInputOne + " Is less than " + userInputTwo);
                System.out.println(comparison);
            }
        }else if (operator == "Print Numbers"){
            comparison = userInputOne + "  " + userInputTwo;
            System.out.println(comparison);
        }else if (operator == "Quit"){
            System.exit(0);
        }


        if (output != -.00001){
        System.out.println(output);
        }

}

}
4

3 回答 3

4

您无法将字符串与==需要使用的字符串进行比较operator.equals()

例如,而不是operator == "Subtract"使用"Subtract".equals(operator)

于 2013-05-02T03:36:54.410 回答
2

对于字符串值比较,您需要使用该String#equals()方法。

==用于对象引用比较。

因此,您所有的operator比较都需要是这样的:-

if ("Add".equals(operator))
于 2013-05-02T03:38:15.437 回答
0

问题是使用==比较字符串。您使用的是哪个 Java 版本?正如其他人所说,您可以equals()改用,但从 Java 7 开始,您可以switch用于字符串比较:

switch (operator) {

    case "Add":
        utput = userInputOne + userInputTwo;
        break;

    case "Subtract":
        utput = userInputOne - userInputTwo;
        break;

    case "Multiply":
        utput = userInputOne * userInputTwo;
        break;

    // ...

    default:
       // optional default statement
       break;
} 

请注意break每个末尾的声明case以防止跌倒。

Oracle 的Java 教程中提供了更多信息(向下滚动到“在 switch 语句中使用字符串”)。

于 2013-05-02T03:55:55.183 回答