I am trying to create a text based calculator. I have a main class and a calc class. The calc class is where everything will happen, and it will be called in the main class. My problem is several variables in my calc class. It is easier to see in code.
import java.util.Scanner;
public class Calc {
String op;
public void operation(String opt){
System.out.println("What operation would you like to perform?");
Scanner operation = new Scanner(System.in);
op = opt;
String op = operation.toString();
getOp(op);
}
public String getOp(String op){
return op;
}
And later on in my code.
public void calculate(){
operation(op);
getNums(1,2);
if(op == "Division"+"division"+"/"){
double value = 1/2;
System.out.println("Your answer is"+value);
}
if(op == "Multiplication"+"multiplication"+"*"){
double value = 1*2;
System.out.println("Your answer is"+value);
}
if(op == "Addition"+"addition"+"+"){
double value = 1+2;
System.out.println("Your answer is"+value);
}
if(op == "Subtraction"+"subtraction"+"-"){
double value = 1/2;
System.out.println("Your answer is"+value);
}
}
My problem is that I can't seem to set the value of op with the scanner, and I don't know if the value of my numbers (1 and 2) have been set either. Any help is greatly appreciated, thank you.
String.equalsmethod for string comparizon instead of==. Don't use theScannerin theCalc, use it inmain. Design theCalcto only take input and calculate.