Как я могу зациклить этот оператор switch? - PullRequest
0 голосов
/ 04 июня 2018

Я делаю программу научного калькулятора на Java, я получил программу для получения необходимых ответов с помощью оператора switch.
Но мне также нужен этот оператор switch для цикла, и у меня было много проблем сthis.
Кто-нибудь может мне помочь?

Это код:

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);

        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();

        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.print("Result: " + result + "Counter: " + counter);
            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.print("Result: " + result + "Counter: " + counter);
            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.print("Result: " + result + "Counter: " + counter);
            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.print("Result: " + result + "Counter: " + counter);
            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.print("Result: " + result + "Counter: " + counter);
            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.print("Result: " + result + "Counter: " + counter);
            case 7:
                System.out.println("Sum of Calculations: " + sumOfcalculations);
                System.out.println("Number of Calculations: " + counter);
                System.out.println("Average of calculations: " + sumOfcalculations/counter);
            default:
                System.out.println("Error: Invalid selection!");
                break;
        }

    }
}

Я знаю, что сделал оператор switch немного слишком сложным, но это был единственный способ, которым ясделал так, как мне нужно.

1 Ответ

0 голосов
/ 04 июня 2018

Использовать реальный механизм петли

...