如何使用循环语句而不是 go to 语句

问题描述

 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 choice = scanner.nextInt();)并再次询问。 "1" -> switch,"2" -> "exit","3" -> 再次询问选择。

解决方法

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

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
        }
    }
}

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

,

试试下面的方法

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

}
,

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

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

然后你就做一些像get_choice(1);

,

试试这个方法:

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

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...