This is all great, but what program do you use to write java? Perhaps you should consider using an IDE, such as Eclipse, as it can automatically detect errors, as well as add imports. (I'm not sure yours does this). He also informs you that the problem is with your program in English. Also, consider this class as an easier and less complicated way to make a calculator:
public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an Operator: "); String in = sc.next(); char oper = in.charAt(0); System.out.print("Enter a number: "); in = sc.next(); double num1 = Double.parseDouble(in); System.out.print("Enter another number: "); in = sc.next(); double num2 = Double.parseDouble(in); if(oper == '+') { double result = num1 + num2; System.out.println(result); } else if(oper == '-') { double result = num1 - num2; System.out.println(result); } else if(oper == 'x') { double result = num1 * num2; System.out.println(result); } else if(oper == '/') { double result = num1 / num2; System.out.println(result); } else { double result = num1 % num2; System.out.println(result); } System.out.println("Hope this helped your mathmatical troubles!"); }
}
And as usual, instead of doing:
import java.util.*;
better to do:
import java.util.Scanner;
This probably doesn't matter much here, but if you use a much larger program importing all java.util, it will slow down your program significantly.
Hope this helps!
source share