Hi everyone!
I'm programming a simple calculator and this is my first time programming. I'm having a bit of difficulty on figuring out this assignment. Everything is working correctly except I need to have the console ask the user if he would like to rerun the program. When the user inputs, "yes" it should start again from the beginning. This is the code I have so far. Do I need to nest all of this code into another loop statement?
import java.util.*; import javax.swing.*; public class SimpleCalculator { public static void main (String [] args){ double result = 0.0, temp = result, oldResult = result; Scanner input = new Scanner(System.in); //Initialization of Calculator System.out.println("Calculator is on"); System.out.println("result = " + result); //Calculations String operation = ""; while (!operation.equals("R") && !operation.equals("r")) { operation = input.nextLine(); //Read operator char sign = operation.charAt(0); //Addition, Subtraction, Multiplication, Division switch (sign){ case '+': temp = Double.parseDouble(operation.substring(1)); oldResult = result; result = result + temp; System.out.println(oldResult + " + " + temp + " = " + result); break; case '-': temp = Double.parseDouble(operation.substring(1)); oldResult = result; result = result - temp; System.out.println(oldResult + " - " + temp + " = " + result); break; case '*': temp = Double.parseDouble(operation.substring(1)); oldResult = result; result = result * temp; System.out.println(oldResult + " * " + temp + " = " + result); break; case '/': temp = Double.parseDouble(operation.substring(1)); oldResult = result; result = result / temp; System.out.println(oldResult + " / " + temp + " = " + result); break; case 'r': break; case 'R': break; default: System.out.println(sign + " is an unknown operation"); System.out.println("Reenter your last line: "); } //END } //Printing of Result System.out.println("Final result = " + result); //Again Method System.out.println("Again?"); String again = input.nextLine(); } }