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

Time:10-21

Hello fellow programmers.

I have the following code: public void command() {

    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.");
        }
    }
}

}

I want it to output a string every fifth time the user has entered a command via the terminal. It would be nice, if it could be made in my "calculator" class, since I wanna ensure I follow GRASP principles.

But if it is to cumbersome, I can live with it being in the "UI" class, which also is where this code is taken from.

I've tried some different loops, but have deleted them all out of anger ._.

Hope someone can help.

CodePudding user response:

Since you care about principles I'll explain what basic math operation will solve the problem of every n-th time do something.

You want to use the modulo operation which returns the (signed) remainder of a division. The modulo operator in Java (and many other programming language) is %.

So for example if you calculate modulo 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 

So in your Java program if you want to do something every 5-th time just check if the modulo 5 operation returns zero.

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

This will print the following:

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

Using that idea it should not be hard to implement what you want.

CodePudding user response:

Thanks for the help.

As alwais, I made it way harder than it should have been. Wanna try to learn how to code the "propper" way from the get go, since I only started a month ago. :D

  • Related