lundi 17 octobre 2016

How to get do-while statement to loop infinitely with nested if statment

I am having a user enter in a multiple of 3 between 3 and 24 inclusive. The output then prints out the number less 3 until it reaches 0. Ex the user picks 15. The output prints out 15,12,9,6,3,0. The problem is if the user picks the number 17 it rounds it down to 15 and proceeds to do the rest of the code. How do I make it repeat the input infinitely if they do not enter in a multiple of 3? My code is as follows.

do{
        System.out.print("Enter a multiple of 3: ");
        //We use the variable n to hold the multiple of 3, like the heading says to do.

        n = input.nextInt();
        if (n % 3 !=0 || n >= 25) {
            System.out.println("Error: Enter a multiple of 3 between 3 and 24, inclusive.");
            n = input.nextInt();                
        }
        /**
         * X = n /3, this gives us the base number of the multiple of 3 to use and figure out the
         * values of n->0 by 3's.
         */
        for(x = n / 3; x <= 8 && x >=0; x--){
            int three = 3 * x;
            System.out.printf(three + "\t");

        }

    }while(x >= 0);

As you can see I just put another input section within the if statement, however I do not wish to do this. I am trying to figure out a way for the if statement to keep looping. Is it my parameters I set up on my if statement? Or is there a specific command to make the if statement repeat if the criteria of the statement is not met? Also I am using Java.

Aucun commentaire:

Enregistrer un commentaire