需要程序每五次输出字符串,而循环已在开关盒中运行

Need program to output string every fifth time while loop has ran withing a switch case

提问人:Andreas Larsen 提问时间:10/21/2022 更新时间:10/21/2022 访问量:32

问:

各位程序员大家好。

我有以下代码: public void 命令() {

    boolean runSystem = true;

    while (runSystem) {




        Scanner sc = new Scanner(System.in);
        //int userInput = sc.nextInt();
        System.out.println("""
        
            1. Division.
            2. Multiplication.
            3. Substraction.
            4. Addition.
            9. Exit program.
            
            Enter command:
                """);

        try {



            switch (sc.nextInt()) {
                case 1:
                    System.out.println("""
                            You have selected division.
                            Please enter the first number you wish to divide, then press enter.
                            Please use comma (,) to seperate periods
                            """);

                    double argOneDivision = sc.nextDouble();
                    System.out.println("Please enter the second number");
                    double argTwoDivision = sc.nextDouble();

                    controller.divisionCalc(argOneDivision, argTwoDivision);

                    System.out.println("We are now dividing " + argOneDivision +  " / " + argTwoDivision + " which equeals: ");
                    System.out.format("%.2f", (controller.divisionCalc(argOneDivision, argTwoDivision)));
                    System.out.println(" ");

                    break;
                case 2:
                    System.out.println("""
                            You have selected multiplciation.
                            Please enter the first number you wish to divide, then press enter.
                            Please use comma (,) to seperate periods
                            """);

                    double argOneMultiplication = sc.nextDouble();
                    System.out.println("Please enter the second number");
                    double argTwoMultiplication = sc.nextDouble();

                    controller.multiplicationCalc(argOneMultiplication, argTwoMultiplication);

                    System.out.println("We are now multiplying " + argOneMultiplication +  " * " + argTwoMultiplication + " which equeals: ");
                    System.out.format("%.2f", (controller.multiplicationCalc(argOneMultiplication, argTwoMultiplication)));
                    System.out.println(" ");
                    break;
                case 3:
                    System.out.println("""
                            You have selected substraction.
                            Please enter the first number you wish to divide, then press enter.
                            Please use comma (,) to seperate periods.
                            To indicate it is a minus number, use - at the start of the number.
                            """);

                    double argOneSubstraction = sc.nextDouble();
                    System.out.println("Please enter the second number");
                    double argTwoSubstraction = sc.nextDouble();

                    controller.substractionCalc(argOneSubstraction, argTwoSubstraction);

                    System.out.println("We are now substracting " + argOneSubstraction +  " - " + argTwoSubstraction + " which equeals: ");
                    System.out.format("%.2f", (controller.substractionCalc(argOneSubstraction, argTwoSubstraction)));
                    System.out.println(" ");
                    break;
                case 4:
                    System.out.println("""
                            You have selected addition.
                            Please enter the first number you wish to divide, then press enter.
                            Please use comma (,) to seperate periods.
                            To indicate it is a minus number, use - at the start of the number.
                            """);

                    double argOneAddition = sc.nextDouble();
                    System.out.println("Please enter the second number");
                    double argTwoAddition = sc.nextDouble();

                    controller.additionCalc(argOneAddition, argTwoAddition);

                    System.out.println("We are now substracting " + argOneAddition +  " + " + argTwoAddition + " which equeals: ");
                    System.out.format("%.2f", (controller.additionCalc(argOneAddition, argTwoAddition)));
                    System.out.println(" ");
                    break;
                case 9:
                    System.out.println("Closing program");
                    runSystem = false;
                    break;
                default:
                    System.out.println("Not a valid option");
                    controller.checkIfInt();
                    break;
            }
        } catch (InputMismatchException e) {
            System.out.println("Invalid input. Please read instrucctions clearly.");
        }
    }
}

}

我希望它每五次输出一个字符串,用户通过终端输入命令。如果能在我的“计算器”课上制作,那就太好了,因为我想确保我遵循 GRASP 原则。

但是,如果它很麻烦,我可以忍受它在“UI”类中,这也是这段代码的来源。

我尝试了一些不同的循环,但出于愤怒而将它们全部删除了._。

希望有人能帮忙。

java while-loop foreach 开关语句

评论


答:

-1赞 Mushroomator 10/21/2022 #1

既然你关心原理,我就解释一下,什么基本的数学运算能解决每n次做某事的问题。

您希望使用模运算,该运算返回除法的(有符号)余数。Java (和许多其他编程语言)中的模运算符是 .%

例如,如果你计算模数 5:

1 % 5 = 1    6 % 5 = 1
2 % 5 = 2    7 % 5 = 2 
3 % 5 = 3    8 % 5 = 3 
4 % 5 = 4    9 % 5 = 4 
5 % 5 = 0   10 % 5 = 0  <== Every 5-th time modulo returns 0 

因此,在您的 Java 程序中,如果您想每 5 次执行一次操作,只需检查模 5 运算是否返回零。

public class Application {

    public static void main(String[] args) {
        for (var i = 1; i <= 17; i++) {
            if(i % 5 == 0) System.out.println("I am a 5-th iteration");
            System.out.println(i);
        }
    }
}

这将打印以下内容:

1
2
3
4
I am a 5-th iteration
5
6
7
8
9
I am a 5-th iteration
10
11
12
13
14
I am a 5-th iteration
15
16
17

使用这个想法,实现你想要的东西应该不难。