I've been constantly getting these errors for a simple calculator I've been trying to make Information:java: Errors occurred while compiling module 'Learning'
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
This is my code
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;
}
}
Thanks for your help guys, also, sorry if i'm not supposed to be posting this, I'm new.
num1
and num2
are not numbers, but of type java.util.Scanner
.
Probably you would to use num1int
and num2int
, as follow:
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;
}
I suggest to change the names to reflect the real types as follow:
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;
}
Note: additionally is not necessary to define two scanners. One is enough.
In your code, you have the line
result = num1 + num2;
where you try to add num1 and num2 together. Given the variable names, this seems reasonable, but when you consider these lines, it becomes ridiculous:
Scanner num1 = new Scanner(System.in);
Scanner num2 = new Scanner(System.in);
You can't add two scanners, that doesn't make sense in theory, and it makes even less sense in practice. I assume that you were trying to do this:
result = num1int + num2int;
This is fine in theory, but when you try do do this operation with num1int = 3 and num2int = 4, you'll get result = 0 instead of result = .75:
result = num1int / num2int;
This is because of integer division, which always rounds towards 0. Put a cast to double to avoid this, like so:
result = ((double) num1int) / num2int;
I hope this helped to clarify your problem. Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.