Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I need help with this program in Java, the program is a scientific calcul

ID: 3905847 • Letter: H

Question

Hello, I need help with this program in Java, the program is a scientific calculator where you select the option you want from a menu on the console. I got the program to give me the answers that I needed with a switch statement. But, I need that switch statement to loop and none of the ways that I have tried to make it loop have worked. can anyone help me? this is my code:

I know that I made the swtich statement a little bit too complicated but I'm new in this and it was the only way I made it work.

thanks in advance!

Explanation / Answer


/**Modified java program for the calculator. Modifications are in bold letters*/

//Main.java
import java.util.Scanner;
import java.lang.Math;
import static java.lang.Math.log10;
public class Main
{
   public static void main(String[] args)
   {
       int opt = 0;
       int counter = 0;
       double firstOperand = 0;
       double secondOperand = 0;
       double sumOfcalculations = 0;
       double result = 0;
       Scanner sc = new Scanner(System.in);

      /**Loop repeats until user enters 0 to quit*/
       while(true)
       {

           System.out.println("Current Result: " + sumOfcalculations);
           System.out.println(" ");
           System.out.println("Calculator Menu");
           System.out.println("---------------");
           System.out.println("0. Exit Program");
           System.out.println("1. Addition");
           System.out.println("2. Subtraction");
           System.out.println("3. Multiplication");
           System.out.println("4. Division");
           System.out.println("5. Exponentiation");
           System.out.println("6. Logarithm");
           System.out.println("7. Display Average");
           System.out.println(" ");
           System.out.print("Enter Menu Selection: ");
           opt = sc.nextInt();

           /*Select the appropriate case selection*/
           switch (opt)
           {
           case 0:
               return;
           case 1:
               System.out.print("Enter first operand: "); // Ask for first operand
               firstOperand = sc.nextDouble(); // Gets first double input
               System.out.print("Enter second operand: "); // Ask for 2nd operand
               secondOperand = sc.nextDouble(); // gets 2nd operand
               result = firstOperand + secondOperand;
               ++counter;
               sumOfcalculations = sumOfcalculations + result;
               System.out.println("Result: " + result );
               System.out.println("Counter: " + counter);
               //add break; statement
               break;

           case 2:
               System.out.print("Enter first operand: "); // Ask for first operand
               firstOperand = sc.nextDouble(); // Gets first double input
               System.out.print("Enter second operand: "); // Ask for 2nd operand
               secondOperand = sc.nextDouble(); // gets 2nd operand
               result = firstOperand - secondOperand;
               ++counter;
               sumOfcalculations = sumOfcalculations + result;
               System.out.println("Result: " + result );
               System.out.println("Counter: " + counter);
               //add break; statement
               break;

           case 3:
               System.out.print("Enter first operand: "); // Ask for first operand
               firstOperand = sc.nextDouble(); // Gets first double input
               System.out.print("Enter second operand: "); // Ask for 2nd operand
               secondOperand = sc.nextDouble(); // gets 2nd operand
               result = firstOperand * secondOperand;
               ++counter;
               sumOfcalculations = sumOfcalculations + result;
               System.out.println("Result: " + result );
               System.out.println("Counter: " + counter);
               //add break; statement
               break;

           case 4:
               System.out.print("Enter first operand: "); // Ask for first operand
               firstOperand = sc.nextDouble(); // Gets first double input
               System.out.print("Enter second operand: "); // Ask for 2nd operand
               secondOperand = sc.nextDouble(); // gets 2nd operand
               result = firstOperand / secondOperand;
               ++counter;
               sumOfcalculations = sumOfcalculations + result;
               System.out.println("Result: " + result );
               System.out.println("Counter: " + counter);
               //add break; statement
               break;

           case 5:
               System.out.print("Enter first operand: "); // Ask for first operand
               firstOperand = sc.nextDouble(); // Gets first double input
               System.out.print("Enter second operand: "); // Ask for 2nd operand
               secondOperand = sc.nextDouble(); // gets 2nd operand
               result = Math.pow(firstOperand, secondOperand);
               ++counter;
               sumOfcalculations = sumOfcalculations + result;
               System.out.println("Result: " + result );
               System.out.println("Counter: " + counter);
               //add break; statement
               break;

           case 6:
               System.out.print("Enter first operand: "); // Ask for first operand
               firstOperand = sc.nextDouble(); // Gets first double input
               System.out.print("Enter second operand: "); // Ask for 2nd operand
               secondOperand = sc.nextDouble(); // gets 2nd operand
               result = (log10(secondOperand)) / (log10(firstOperand));
               ++counter;
               sumOfcalculations = sumOfcalculations + result;
               System.out.println("Result: " + result );
               System.out.println("Counter: " + counter);
               //add break; statement
               break;

           case 7:
               System.out.println("Sum of Calculations: " + sumOfcalculations);
               System.out.println("Number of Calculations: " + counter);
               System.out.println("Average of calculations: " + sumOfcalculations/counter);
               //add break; statement
               break;

           default:
               System.out.println("Error: Invalid selection!");
               //add break; statement
               break;
           } //end of switch case
       } //end of while loop
   }//end of the main method
}//end of the Main class

------------------------------------------------

Sample Output:

Current Result: 0.0

Calculator Menu
---------------
0. Exit Program
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exponentiation
6. Logarithm
7. Display Average

Enter Menu Selection: 1
Enter first operand: 1
Enter second operand: 1
Result: 2.0
Counter: 1
Current Result: 2.0

Calculator Menu
---------------
0. Exit Program
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exponentiation
6. Logarithm
7. Display Average

Enter Menu Selection: 2
Enter first operand: 2
Enter second operand: 1
Result: 1.0
Counter: 2
Current Result: 3.0

Calculator Menu
---------------
0. Exit Program
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exponentiation
6. Logarithm
7. Display Average

Enter Menu Selection: 3
Enter first operand: 1
Enter second operand: 2
Result: 2.0
Counter: 3
Current Result: 5.0

Calculator Menu
---------------
0. Exit Program
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exponentiation
6. Logarithm
7. Display Average

Enter Menu Selection: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote