【问题标题】:While loop in method JavaJava方法中的while循环
【发布时间】:2018-01-06 14:19:46
【问题描述】:

我想实现这个菜单,以便在我输入 1、2、3 以外的错误输入时保持循环以接收输入。如何以及在何处放置我的 while 循环/执行 while 循环?我是 JAVA 新手。

在用户输入 1,2 或 3 以外的内容后,它应该再次提示菜单。我可以知道怎么做吗?谢谢。

如何以及在何处放置我的 while 循环/执行 while 循环?

import java.util.*;
public class InputMenu 
{

public void display_menu() 
{

System.out.println("1) Option 1\n2) Option 2\n3) Option 3");
System.out.print("Selection: ");
}

public void question()
{
System.out.println("Would you like to proceed or quit?");
System.out.println("To proceed enter 9.");
System.out.println("If you wish to quit enter 0.");
Scanner q = new Scanner(System.in);

switch (q.nextInt()) 
{
    case 0:
    System.out.println ("Thank you and goodbye.");
    break;

    case 9:
    System.out.println ("Please proceed.");
    new InputMenu();
    break;
    default:
    System.err.println ( "Unrecognized option" );
        reenter();
    break;
}
}

public void reenter(){
    System.out.println ("Please re-enter option from 1 - 3 only.");
    display_menu();
}

public InputMenu() 
{
Scanner in = new Scanner(System.in);
    display_menu();


switch (in.nextInt()) 
{
    case 1:
    System.out.println ( "You picked option 1" );
    question();
    break;

    case 2:
    System.out.println ( "You picked option 2" );
    question();
    break;

    case 3:
    System.out.println ( "You picked option 3" );
    question();
    break;
    default:
    System.err.println ( "Unrecognized option" );
        reenter();
    break;
}
}

public static void main (String[]args) 
{
new InputMenu();
}
}

【问题讨论】:

标签: java loops while-loop do-while


【解决方案1】:

可能在您的构造函数中

public InputMenu() 
{
    Scanner in = new Scanner(System.in);
     display_menu();

    do{       //here
        int input = in.nextInt();
        switch (input) 
        {
            case 1:
            System.out.println ( "You picked option 1" );
            question();
            break;

            case 2:
            System.out.println ( "You picked option 2" );
            question();
            break;

            case 3:
            System.out.println ( "You picked option 3" );
            question();
            break;
            default:
            System.err.println ( "Unrecognized option" );
                reenter();
       }
    }
    while(input>3 || input <1); //here

}

【讨论】:

  • 我在 do while for (input>3 || input
  • 已解决。谢谢大佬^^
【解决方案2】:

您可以使用下面的代码输入valid指定的一些整数。

public static int read(String hint, Scanner scanner, Integer... valid) {
    System.out.print(hint);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        try {
            int input = Integer.parseInt(line);
            if (Arrays.asList(valid).contains(input))
                return input;
        } catch (NumberFormatException e) {}
        System.out.print(hint);
    }
    return -1; // This is unreachable
}

然后你打电话

int selection = read("Selection: ", in, 1, 2, 3);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多