0

如果用户没有输入给定类型的操作之一,我正在尝试制作一个简单的计算器并在尝试使其显示“错误”时继续在标题中显示错误。

import java.util.Scanner;

public class experiments {
    public static void main(String args[]) {
        Scanner userInput = new Scanner(System.in);
        String operation;
        double fNum, sNum, ans;

        //select type of operation
        System.out.println("Type addition, subtraction, multiplication, or division, then press enter");
        operation = userInput.nextLine();
        if (operation!=("addition","subtraction","multiplication","division")) {
            System.out.println("error");
        }

        //enter numbers
        System.out.println("Enter first number");
        fNum = userInput.nextDouble();
        System.out.println("Enter second number");
        sNum = userInput.nextDouble();

        //calculate
        if (operation.equals("addition")) {
            ans=fNum + sNum;
        }
        else if (operation.equals("subtraction")) { 
            ans=fNum - sNum;
        }
        else if (operation.equals("multiplication")){
            ans=fNum * sNum;
        }
        else if (operation.equals("division")) {
            ans=fNum/sNum;
        }
        //print answer
        System.out.println("The answer is ");
        System.out.println(ans);    
    }
}
4

7 回答 7

2

您不能将事物与这样的一组对象进行比较:

operation!=("addition","subtraction","multiplication","division")

大概你想要的是“如果operation不是这四件事之一”。你有几个选择。最像您现在拥有的一个是创建一个包含您的四个合法操作的新ArrayList(例如legalOperations),然后使用legalOperations.contains(operation).

但是,一种更简洁的方法,即“更好的 Java”,将是进行enum并使用它来进行比较。

public enum LegalOperations {
    ADDITION,
    SUBTRACTION,
    MULTIPLICATION,
    DIVISION
}

然后你可以对你的枚举进行比较(例如,也许你会给枚举一个构造函数以允许它为每个枚举常量和一个isLegalOperation方法都有一个明确的字符串值)。

于 2013-06-27T04:00:09.083 回答
1

Java无法理解这个命令......

if (operation!=("addition","subtraction","multiplication","division")) {

相反,您需要单独检查每个...

if (!"addition".equals(operation) &&
    !"subtraction".equals(operation) &&
    !"multiplication".equals(operation) &&
    !"division".equals(operation)) {
    // Handle error...
}
于 2013-06-27T04:00:00.557 回答
1

你应该ArrayList在那里创造和创造价值。
然后您可以检查该值是否存在于ArrayList.
像这样:
Arrays.asList(...).contains(...) 如何工作?

于 2013-06-27T04:00:02.557 回答
0

if (operation!=("addition","subtraction","multiplication","division"))在 Java 中是不正确的比较。switch如果您使用 Java 7 或带有string.equals多个 if 语句的方法,请使用语句。

于 2013-06-27T04:00:47.757 回答
0

我认为您可以重写为

if (!(    operation.equals("addition") || operation.equals("subtraction") ||
          operation.equals("multiplication") || operation.equals("division"))) {
                System.out.println("error");
        }
于 2013-06-27T04:02:42.373 回答
0

还有另一种简单的方法,它涉及减少编写

因为你有一个字符串数组,所以将它声明为一个数组

String [] operations = {"addition", "subtraction", "multiplication", "division"};

然后用它来检查

 if(!Arrays.asList(operations).contains(operation)) {
   System.out.println("error");
 }
于 2013-06-27T04:07:56.643 回答
0

您可以像这样使用 contains 方法:

String[] operations = {"addition","subtraction","multiplication","division"};
if(!operations.contains(operation)) {
    ...
于 2013-06-27T04:02:18.930 回答