简单的java计算器错误

时间:2013-12-13 07:07:04

标签: java calculator

您好我最近在2天前对java感兴趣,在观看一些教程时,我决定在看到一个超级简单的加法计算示例后制作一个计算器。我想知道我在哪里错了。 PLZ帮助我超级新的。(如果有所作为,我也使用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)

4 个答案:

答案 0 :(得分:1)

这些问题

signs(); = operation.nextInt(); 


if (problem = signs()){

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

您最好使用equals()problem.equals(signs())

答案 1 :(得分:0)

问题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为字符串,因此无法进行转换。
  • 这个方法的返回类型是int,它总是返回0所以返回类型需要什么作为int。它必须是无效的(这不是问题,但是为了最佳实践)

答案 2 :(得分:0)

问题在于这两行:

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;

答案 3 :(得分: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;
  //    }
  }