繁体   English   中英

在Java中遇到麻烦的循环

[英]Having trouble with loop in java

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

    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之后,程序将返回到开头并再次询问选择用户想要的选项。 我该如何使其正常工作? 另外,如果我看不到的代码还有其他问题,请告诉我。 谢谢

您需要将这整堆代码放入while循环中,这将始终是正确的,最后要求用户再次输入。 如果用户按4(我认为是退出),只需在while循环中使用break退出程序。

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

您可以尝试这样。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM