【问题标题】:simple java calculator errors简单的java计算器错误
【发布时间】:2013-12-31 21:25:51
【问题描述】:

您好,我前两天对 java 产生了兴趣,在看一些教程时,在看到一个超级简单的加法计算示例完成后,我决定制作一个计算器。我想知道我哪里出错了。请帮助我对它超级陌生。(如果有影响的话,我也使用 eclipse idk)

import java.util.Scanner;

public class calculator {
public static void main(String args[]){
    Scanner operation = new Scanner(System.in);
    Scanner number = new Scanner(System.in);
    int x;
    int y;
    int problem = multiplication, division, addition, subtraction;
    int answer = answerM, answerD, answerA, answerS;


    System.out.println("enter first number: "); 
    x = number.nextInt();
    System.out.println("enter operator: "); 
    signs(); = operation.nextInt();     
    System.out.println("enter second number: ");
    y = number.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;
                }
            }
        }
    }
}
private static int signs() {
    multiplication = "*";
    division = "/";
    addition = "+";
    subtraction = "-";
    return 0;
}
}

错误,错误

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)

【问题讨论】:

  • 在开始编写计算器之前,请花一些时间阅读语言参考。像int problem = multiplication, division, addition, subtraction; 这样的语句表明你需要一些bit阅读。
  • @devnull,如果之前声明了 multiplication,它将起作用;)
  • 问题太多了。开始从文档中学习或购买一本好书。
  • “一边看教程”。我会推荐阅读一些书籍。 YouTube 教程遗漏了很多细节。
  • 伟大的 Java 教科书“Java 编程入门”,作者 Daniel Liang。我相信最新版本是第 9 版 :)

标签: java calculator


【解决方案1】:

这几行有问题

signs(); = operation.nextInt(); 


if (problem = signs()){

它将signs()的返回值分配给变量problem

您可以更好地使用equals()problem.equals(signs())

【讨论】:

  • 很酷,谢谢。我会尝试,在像这样匆忙进入之前阅读更多关于 java 的内容。
  • @user3097986 好的 Gud...继续前进:-)
【解决方案2】:

问题 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;
  • problem 是 int,multiplication 是 string,所以这种转换是不可能的。
  • 这个方法的返回类型是int,它总是返回0,所以需要返回类型为int。它必须是无效的(这不是问题,而是最佳实践)

【讨论】:

    【解决方案3】:

    问题在于这两行:

    int problem = multiplication, division, addition, subtraction;
    int answer = answerM, answerD, answerA, answerS;
    

    您已经声明了int problem = multiplication,但没有在任何地方定义乘法。 同样,在声明中int answer = answerM 也没有定义。

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

    int answerM = 0;
    int multiplication = 0;
    

    【讨论】:

    • 哦,好的,我知道我在那部分哪里出错了。谢谢大佬,我一定要改。
    【解决方案4】:

    检查此代码.. 这工作正常。检查您的错误并查看解决方案。

    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 x和int y,是否应该是public int x;和 public int y;
    • 它们在一个方法中。 (int main 方法),因此它们仅在该方法中可见。没有必要将它们公开。检查this问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 2011-02-13
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多