Hello,
After a relatively small C++ background, I decided to learn some Java. I want to be able to pass two variables from my main class, evaluate them in another class, and return that answer to the main class. I have all the code done for it pretty much, but I can't figure out how to use the returned variable in my main class. Any help would be appreciated!
//This is the main class file
import java.util.Scanner;
public class Main {
public static void main (String args[]) {
System.out.println("Calculator v2");
Scanner choice = new Scanner (System.in);
System.out.println("Which operation do you want to do?");
System.out.println("1-Addition");
System.out.println("2-Subtraction");
System.out.println("3-Multiplication");
System.out.println("4-Divistion");
int opt = choice.nextInt();
switch (opt) {
case 1: {
Addition addobj = new Addition();
System.out.println("Enter the first number!");
Scanner numbers = new Scanner (System.in);
double fnum = numbers.nextDouble();
System.out.println("Enter the first number!");
double snum = numbers.nextDouble();
addobj.add(fnum, snum);
break;
}
default: {
System.out.println("No such operation!");
}
}
}
}
//This is the addition class file
public class Addition {
public static double add (double num, double num2) {
double answer = num+num2;
return answer;
}
}
I just tried adding a "System.out.println(answer)" in the case inside the switch statement (ln. 22) , but I got an error saying that "answer cannot be resolved to a variable." I'm sure this is a quick fix, but I couldn't find anything helpful by googling it, so any help would be awesome.
Thanks much!
~Carpetfizz