简单java计算器错误

简单java计算器错误,java,calculator,Java,Calculator,您好,我最近在两天前对java产生了兴趣,在观看一些教程时,在看到一个超级简单的加法计算器示例完成后,我决定制作一个计算器。我在想我在哪里错了。plz help我对它非常陌生。(如果有区别的话,我也会使用EclipseIDK) 错误,错误 Exception in thread "main" java.lang.Error: Unresolved compilation problems: multiplication cannot be resolved to a variable

您好,我最近在两天前对java产生了兴趣,在观看一些教程时,在看到一个超级简单的加法计算器示例完成后,我决定制作一个计算器。我在想我在哪里错了。plz help我对它非常陌生。(如果有区别的话,我也会使用EclipseIDK)

错误,错误

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    multiplication cannot be resolved to a variable
    answerM cannot be resolved to a variable
    Syntax error on token "=", delete this token
    answerM cannot be resolved to a variable
    Type mismatch: cannot convert from int to boolean

    at calculator.main(calculator.java:10)
这些方面的问题

signs(); = operation.nextInt(); 

它将为变量
问题
分配
符号()的返回值


您可以更好地使用
equals()
ie
problem.equals(signs())
问题1

int problem = multiplication, division, addition, subtraction;
问题2

int answer = answerM, answerD, answerA, answerS;
问题3

signs(); = operation.nextInt();
要记住的事情:

private static int signs() {
    multiplication = "*";
    division = "/";
    addition = "+";
    subtraction = "-";
    return 0;
}
这表明乘法、除法、加法都是你写的字符串

int problem = multiplication, division, addition, subtraction;
  • 问题
    是int,而
    乘法
    是string,因此无法进行此转换
  • 此方法返回类型为int,并且始终返回0,因此返回类型需要为int。它必须为void(这不是问题,而是为了最佳实践)

    • 问题是由以下两条线路引起的:

      int problem = multiplication, division, addition, subtraction;
      int answer = answerM, answerD, answerA, answerS;
      
      您已经声明了
      int problem=multiply
      ,但乘法没有在任何地方定义。 同样,声明中未定义
      int-answerM=answerM

      您可以通过声明以下两个int变量来删除这些错误:

      int answerM = 0;
      int multiplication = 0;
      

      请检查此代码。。这很好用。检查错误并查看解决方案

      import java.util.Scanner;
      
      public class calculator {
      
      public static void main(String args[]) {
          // no need of two Scanners to read the same stream
          //   Scanner operation = new Scanner(System.in);
          //  Scanner number = new Scanner(System.in);
          Scanner input = new Scanner(System.in);
          int x;
          int y;
          // can't declare variables like this in java
          //        int problem = multiplication, division, addition, subtraction;
          //        int answer = answerM, answerD, answerA, answerS;
          int answer = 0;
      
          System.out.println("enter first number: ");
          x = input.nextInt();
          System.out.println("enter operator: ");
          // you can't assign a value to a method call
          //signs(); = operation.nextInt();
          String sign = input.next();
          System.out.println("enter second number: ");
          y = input.nextInt();
          //        System.out.println(answer);
      
      
         //        if (problem == signs()) {
         //            answerM = x * y;
         //        } else {
         //            if (problem = signs()) {
        //                answerD = x / y;
        //            } else {
        //                if (problem == signs()) {
        //                    answerA = x + y;
        //                } else {
        //                    if (problem == signs()) {
        //                        answerS = x - y;
        //                    }
        //                }
        //            }
        //        }
          if (sign.equals("*")) {
              answer = x * y;
          } else {
              if (sign.equals("/")) {
                  answer = x / y;
              } else {
                  if (sign.equals("+")) {
                      answer = x + y;
                  } else {
                      if (sign.equals("-")) {
                          answer = x - y;
                      }
                  }
              }
          }
          System.out.println(answer);
      
      }
         //This method  always return '0'
        //    private static int signs() {
        //        multiplication = "*";
        //        division = "/";
        //        addition = "+";
        //        subtraction = "-";
        //        return 0;
        //    }
        }
      

      在开始编写计算器之前,请花些时间阅读语言参考资料。像
      int problem=乘法、除法、加法、减法之类的语句表示您需要一些读取。@devnull,如果前面声明了一个
      乘法,
      ,它就可以工作了;)问题太多了。从文档开始学习,或者买一本好书,“一边看教程”。我建议你读一些书。YouTube教程遗漏了很多细节。丹尼尔·梁的优秀Java教科书《Java编程入门》。我相信最新的版本是第9版:)很酷,谢谢。我会尝试一下,然后在像这样急匆匆进入之前多读一点java。@user3097986好的,古德……继续:-)哦,好的,我知道我在这一部分错在哪里了。谢谢,伙计,我一定会换的。非常感谢。感谢您分析和解释int x和int y的每个部分,如果它是公共int x;和公众智力;它们在一个方法中。(int main方法)因此它们仅在该方法中可见。把它们公之于众毫无意义。检查问题。
      int answerM = 0;
      int multiplication = 0;
      
      import java.util.Scanner;
      
      public class calculator {
      
      public static void main(String args[]) {
          // no need of two Scanners to read the same stream
          //   Scanner operation = new Scanner(System.in);
          //  Scanner number = new Scanner(System.in);
          Scanner input = new Scanner(System.in);
          int x;
          int y;
          // can't declare variables like this in java
          //        int problem = multiplication, division, addition, subtraction;
          //        int answer = answerM, answerD, answerA, answerS;
          int answer = 0;
      
          System.out.println("enter first number: ");
          x = input.nextInt();
          System.out.println("enter operator: ");
          // you can't assign a value to a method call
          //signs(); = operation.nextInt();
          String sign = input.next();
          System.out.println("enter second number: ");
          y = input.nextInt();
          //        System.out.println(answer);
      
      
         //        if (problem == signs()) {
         //            answerM = x * y;
         //        } else {
         //            if (problem = signs()) {
        //                answerD = x / y;
        //            } else {
        //                if (problem == signs()) {
        //                    answerA = x + y;
        //                } else {
        //                    if (problem == signs()) {
        //                        answerS = x - y;
        //                    }
        //                }
        //            }
        //        }
          if (sign.equals("*")) {
              answer = x * y;
          } else {
              if (sign.equals("/")) {
                  answer = x / y;
              } else {
                  if (sign.equals("+")) {
                      answer = x + y;
                  } else {
                      if (sign.equals("-")) {
                          answer = x - y;
                      }
                  }
              }
          }
          System.out.println(answer);
      
      }
         //This method  always return '0'
        //    private static int signs() {
        //        multiplication = "*";
        //        division = "/";
        //        addition = "+";
        //        subtraction = "-";
        //        return 0;
        //    }
        }