在java中遇到循环问题

时间:2015-04-09 02:30:39

标签: java arrays loops if-statement

我试图在用户完成其中一个选项后让我的代码循环回程序的开头。我似乎无法弄清楚如何让它正常工作。这是我到目前为止的代码

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

基本上我需要它,以便在用户输入2并完成选项2后,程序将返回到开头并再次询问选择用户想要的选项。我怎样才能让它正常工作?如果我没看到的代码有任何其他问题,请告诉我。感谢

3 个答案:

答案 0 :(得分:1)

你需要的是将一大堆代码放在while循环中,这将始终为true并在结束时再次询问用户输入。如果用户按下4,我认为是退出,只需在while循环中使用break退出程序。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

你可以尝试这样的事情。

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