【问题标题】:How can I loop this switch statement?如何循环这个 switch 语句?
【发布时间】:2018-06-04 01:07:52
【问题描述】:

我正在用 Java 编写一个科学计算器程序,我得到的程序可以通过 switch 语句获得所需的答案。
但我也需要那个 switch 语句来循环,我对此有很多问题。
谁能帮帮我?

这是代码:

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 语句有点过于复杂,但这是我让它按需要工作的唯一方法。

【问题讨论】:

  • 需要该 switch 语句循环是什么意思?,当一个条件应该为真时使用 Switch 语句。
  • @LuaiGhunim 我相信问题希望整个方法循环。菜单选择,以及下面的开关
  • 在 opt = sc.nextInt() 之前使用一段时间(opt != 0);并在 while 范围之外的末尾返回 0。同时删除大小写 0,或使用其他数字(如 -1)退出程序
  • 不是应该在每一个case之后都休息一下吗?

标签: java loops switch-statement


【解决方案1】:

使用实际的循环机制

Scanner sc = new Scanner(System.in);
String input;
while (true) {
   input = sc.nextLine();
   if (input.equals("0")) break;

   menu(Integer.parseInt(input)); // Make 'static void menu(int op)' a separate method, not dump everything in the main method
}
System.out.println("Goodbye!");
return;

您还需要休息一下

case 1:
   addition(); // TODO: define this
   break;
case 2:
   subtraction(); // TODO: define this
   break;
// ... etc
default:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多