【问题标题】:How do I use loop statement instead of go to statement如何使用循环语句而不是 go to 语句
【发布时间】:2021-04-16 13:46:44
【问题描述】:
 while(true)
    {
  
        switch (...)
        {
            case 1:
                //some code
                break;
            case 2:
                //some code
                break;
        }


        System.out.println("(1)Continue (2)Exit");

   //example:

        int choice = scanner.nextInt();

        if (choice == 1)
            continue; //it should go to switch

        else if (choice == 2)
        {
            System.out.println("Exit..."); // should exit
            break;
        }

 
        else
        {
            System.out.println("Wrong num, try it again!"); // should ask choose again until we enter 1 or 2

            //goto example;
        }

    }

我的问题是:当我选择错误的数字时,它会进入循环的开头。我希望它转到询问选择的部分(int selection =scanner.nextInt();)并再次询问。 "1" -> switch, "2" -> "exit" , "3" -> 再次询问选择。

【问题讨论】:

  • 把循环放在你想循环的位周围:如果你不想回到开关,要么在开关后移动while;或者在输入和 if/else 周围放置另一个循环。
  • 您永远不需要goto 声明。如前所述:循环正确的事情,然后适当地使用break / continue。

标签: java goto


【解决方案1】:

像下面这样试试

while(true)
{

    int choice = scanner.nextInt();

    if (choice == 1)
        continue;

    else if (choice == 2)
    {
        System.out.println("Exit...");
        break;
    }

    else
    {
        System.out.println("Wrong num, try it again!");
        //goto example;
    }

}

【讨论】:

    【解决方案2】:

    这应该可以解决您的问题。

    import java.util.Scanner;
    
    class Main
    {
        private static void switcher(int key)
        {
            switch (key)
            {
                case 1:
                    //some code
                    break;
                case 2:
                    //some code
                    break;
            }
        }
        
        public static void main (String[] args)
        {
            Scanner sc = new Scanner(System.in);
            switcher(0);//replace with whatever you need
            System.out.println("(1)Continue (2)Exit");
            while(true)
            {
                int choice = sc.nextInt();
                if (choice == 1)
                    continue;
                else if (choice == 2)
                {
                    System.out.println("Exit...");
                    break;
                }
                else
                {
                    System.out.println("Wrong num, try it again!");
                }
                switcher(0);//replace with whatever you need
            }
        }
    }
    

    如果您有其他要求,请更新您的问题。

    【讨论】:

    • 当我这样做时,它再也不会去switch了。例如,在这段代码中,当我选择“1”时,它会循环但不会切换。事情是“1” -> switch,“2” -> 退出,“3” -> 再次询问选择,直到输入 1 或 2。
    • 代码已更新。看看您的问题是否已解决。
    【解决方案3】:

    你可以尝试做这样的事情,也许有点难看,但我认为它会起作用:

            function get_choice(choice) {
                if (choice == 1) {
                    // do something
                } else if (choice == 2) {
                    // do something else
                } else {
                    // something else
                }
            }
    

    然后你就做类似get_choice(1);

    【讨论】:

      【解决方案4】:

      试试这个方法:

      import java.util.Scanner;
      
      public class Test {
      
          public static void main(String[] args) {
              int choice;
              do {
                  System.out.println("Enter your choice :");
                  System.out.println("1. Start program");
                  System.out.println("2. End   program");
                  Scanner sc = new Scanner(System.in);
                  choice = sc.nextInt();
              } while (choice != 2);
              System.out.println("Exit application :");
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-17
        • 1970-01-01
        • 2011-09-14
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 2017-09-28
        • 2013-03-08
        相关资源
        最近更新 更多