提问者:小点点

I不能等于if语句[closed]


这个问题是由一个打字错误或一个无法重现的问题引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能对未来的读者有所帮助。

大家好,我是Java的新手。我尝试用Java做一个计算器,但遇到了这个问题:在我的if语句中,它不等于一个变量和一个算术符号。我想知道为什么?我在下面用粗体标记了问题。

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner TB = new Scanner(System.in);

        System.out.println("Choose your number");
        double number = TB.nextDouble();

        System.out.println("Choose a basic arithmetic: '+','-','*','/' or '%'. ");
        char basicarithmetic = TB.next().charAt(0);

        System.out.println("Choose a number again");
        double number2 = TB.nextDouble();

        if (basicarithmetic == **+**) {
            System.out.println(number + number2);
        } else if (basicarithmetic == **-**) {
            System.out.println(number - number2);
        } else if (basicarithmetic == *****) {
            System.out.println(number * number2);
        } else if (basicarithmetic == **/**) {
            System.out.println(number / number2);
        } else if (basicarithmetic == **%**) {
            System.out.println(number % number2);
        }           

        


    }
}

共1个答案

匿名用户

你的问题相当简单。您正在尝试比较字符,但您忘记了将它们放入< code>''中。

编辑:我错过了一个重要的细节(谢谢你:)

您正在尝试比较字符。

如果您将if更改为:

if (basicarithmetic == '+') {

} else if (basicarithmetic == '-') {

} // etc

那么你的代码就可以工作了*不是注释字符串(或字符)的方式。