import java.util.Scanner;
public class Calculator {
static double num1 = 0, num2 = 0;
static Scanner input = new Scanner(System.in);
static int choice = 0;
static double answer = 0;
public static void main(String args []){
enter(num1, num2);
}
public static void enter(double num1, double num2) {
System.out.println("********CALCULATOR*********");
System.out.println("Type in your first number.");
num1 = input.nextDouble();
System.out.println("Type in your second number.");
num2 = input.nextDouble();
System.out.println("Enter the number associated with what you would like to do.");
System.out.println("1. Add them");
System.out.println("2. Subtract them");
System.out.println("3. Multiply them");
System.out.println("4. Divide them");
choice = input.nextInt();
operations(num1, num2, choice);
}
public static void operations(double num1, double num2, int choice) {
if(choice == 1){
answer = num1 + num2;
}else if(choice == 2){
answer = num1 - num2;
}else if(choice == 3){
answer = num1 * num2;
}else if(choice == 4){
answer = num1 / num2;
}
printAnswer(answer);
}
public static void printAnswer(double answer) {
boolean re;
String tryAgain;
System.out.println("The answer is " + answer);
System.out.println("Would you like to use it again? Yes or No.");
tryAgain = input.next();
if(tryAgain == "yes"){
re = true;
restart(re);
}else if (tryAgain == "no"){
re = false;
restart(re);
}
}
public static void restart(boolean re) {
if(re == true){
enter(num1, num2);
}
if (re == false){
System.exit(0);
}
}
}
I'm trying to make a calculator, but I ran into a little problem... The problem is occurring after
tryAgain = input.next();
I don't think the rest of my code is being run The Scanner works properly but the program quits after I type something in.