Will
Will

Reputation: 31

Having trouble with loop in java

I am trying to get my code to loop back to the beginning of the program after the user completes one of the options. I cannot seem to figure out how to get it to work properly. Here is my code so far

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int num1;
        int num2;
        int num3;

        boolean opt1Done = false;
        System.out.println("Select your next step");
        System.out.println("1: Enter three numbers between 1 and 100.");
        System.out.println("2: Order your number in ascending order");
        System.out.println("3: Determine if the three inputs form a triangle");
        System.out.println("4: Exit");

        int answer = console.nextInt();
        num1 = console.nextInt();
        num2 = console.nextInt();
        num3 = console.nextInt();

        if (answer == 1) {
            //do whatever for option 1
            System.out.println("Enter a value for num1 between 1 and 100.");
            System.out.println("Enter a value for num2 between 1 and 100.");
            System.out.println("Enter a value for num3 between 1 and 100.");

            opt1Done = true;
      } else if (answer == 2) {
        if (opt1Done) {
                //...... do whatever to order the numbers
            int[] arraynum;
            arraynum = new int[3];

            arraynum[0] = num1;
            arraynum[1] = num2;
            arraynum[2] = num3;

            Arrays.sort(arraynum);

            int i;

            for (i=0; i < arraynum.length; i++) {
                System.out.println("num:" + arraynum[i]);
            }

            } else {
                System.out.println("you must complete Step 1 before Step 2");
            }
      } else if (answer == 3) {
        if (opt1Done) {
                //... do whatever to determine if triangle or not
                if (num1+num2>num3 && num1+num3>num2 && num2+num3>num1)
                {   
                    System.out.print("TRIANGLE");
                }
                else
                {   
                    System.out.print("NO TRIANGLE");
                }
            } else {
                System.out.println("you must complete Step 1 before Step 3");
            }
        }
    }
}

Basically I need it so that after the user enters 2 and completes option 2, the program will then go back to the beginning and ask again to choose which option the user wants. How can I get this to work properly? Also if anything else is wrong with the code that I do not see please let me know. Thanks

Upvotes: 0

Views: 69

Answers (3)

MP7
MP7

Reputation: 11

You can try something like this.

Boolean p = true;
while(p){
        System.out.println(
            " Select an option:\n" +
            "1: Enter three numbers between 1 and 100.\n" +
            "2: Order your number in ascending order\n"+
            "3: Determine if the three inputs form a triangle\n"+
            "4: Exit\n"
         );
         switch(){
           case 1:
                .
                .
                .
           case 4:
               System.exit();
          }
}

Upvotes: 0

radically
radically

Reputation: 56

int input;
do {
 //---
 //The rest of your logic
 //---
} while(input != 4);

Upvotes: 1

Viraj Nalawade
Viraj Nalawade

Reputation: 3225

What you need is put this whole bunch of code in while loop which will always be true and ask the user input again at end. If user presses 4 which i think is exit just exit the program by using break in your while loop.

Upvotes: 1

Related Questions