使用简单的计算器遇到麻烦我一直试图制作

时间:2017-07-27 16:44:44

标签: java

我一直在为一个我一直试图制作的简单计算器而经常出现这些错误      信息:java:编译模块'学习'

时出错
Error:(24, 31) java: bad operand types for binary operator '*'
 first type:  java.util.Scanner
  second type: java.util.Scanner


Error:(27, 31) java: bad operand types for binary operator '/'
first type:  java.util.Scanner
second type: java.util.Scanner


 Error:(21, 31) java: bad operand types for binary operator '-'
 first type:  java.util.Scanner
 second type: java.util.Scanner


Error:(18, 31) java: bad operand types for binary operator '+'
 first type:  java.util.Scanner
  second type: java.util.Scanner


Error:(16, 16) java: incompatible types: java.util.Scanner cannot be converted to int

这是我的代码

import java.util.Scanner;

class Main {

public static void main(String[] args) {
    Scanner Operation = new Scanner(System.in);
    Scanner num1 = new Scanner(System.in);
    Scanner num2 = new Scanner(System.in);
    float result = 0;

    System.out.println("What is your first number?");
    int num1int = num1.nextInt();
    System.out.println("What is your second number?");
    int num2int = num2.nextInt();
    System.out.println("What operation would you like to perform?");
    switch (Operation) {
        case "addition":
            result = num1 + num2;
            break;
        case "subtraction":
            result = num1 - num2;
            break;
        case "multiplication":
            result = num1 * num2;
            break;
        case "division":
            result = num1 / num2;
            break;
    }

}

感谢您的帮助,对不起,如果我不应该发布这个,我是新的。

2 个答案:

答案 0 :(得分:3)

num1num2不是数字,而是类型java.util.Scanner

您可能会使用num1intnum2int,如下所示:

 switch (Operation) {
    case "addition":
        result = num1int + num2int;
        break;
    case "subtraction":
        result = num1int - num2int;
        break;
    case "multiplication":
        result = num1int * num2int;
        break;
    case "division":
        result = num1int / num2int;
        break;
}

我建议更改名称以反映真实类型,如下所示:

Scanner scannerNum1 = new Scanner(System.in);
Scanner scannerNum2 = new Scanner(System.in);
float result = 0;

System.out.println("What is your first number?");
int num1 = scannerNum1.nextInt();
System.out.println("What is your second number?");
int num2 = scannerNum2.nextInt();
switch (Operation) {
    case "addition":
        result = num1 + num2;
        break;
    case "subtraction":
        result = num1 - num2;
        break;
    case "multiplication":
        result = num1 * num2;
        break;
    case "division":
        result = num1 / num2;
        break;
}

注意:另外不需要定义两个扫描仪。一个就够了。

答案 1 :(得分:1)

在您的代码中,您有

result = num1 + num2;

您尝试将num1和num2一起添加。鉴于变量名称,这似乎是合理的,但是当你考虑这些行时,它就变得荒谬了:

Scanner num1 = new Scanner(System.in);
Scanner num2 = new Scanner(System.in);

你不能添加两个扫描仪,这在理论上没有意义,而且在实践中它的含义更少。我假设你试图这样做:

result = num1int + num2int;

理论上这很好,但是当你尝试使用num1int = 3和num2int = 4进行此操作时,你得到的结果= 0而不是result = .75:

result = num1int / num2int;

这是因为整数除法总是向0舍入。将一个转换加倍以避免这种情况,如下所示:

result = ((double) num1int) / num2int;

我希望这有助于澄清您的问题。祝你好运!