简体   繁体   English

如果用户输入不正确,如何让问题循环

[英]How to get the question to loop if the user input is incorrect

Whenever the user enters data that is incorrect, the program puts out a message telling the user to try again, but it then goes back to the main menu instead of letting the user try an input a different value. 每当用户输入不正确的数据时,该程序都会发出一条消息,告知用户再次尝试,但随后返回主菜单,而不是让用户尝试输入其他值。 How can I do this? 我怎样才能做到这一点?

public void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    in.nextLine();
    inputStudentID = in.nextLine();
    if (!(inputStudentID.length()==6))
    {
        System.out.println("Enter a student ID that is 6 characters");
    }
    else
    {
        System.out.println("Enter Module Mark: ");
        inputMark = in.nextInt();
        if (inputMark <0 || inputMark >100)
        {
            System.out.println("PLease enter a module mark between 0-100");
        }
        else
        {
            addStudent(inputStudentID, inputMark);
            System.out.println();
            System.out.println("New student record has been " + 
                        "successfully created");
            System.out.println();
            countRecords();
        }
    }
}

One way to do it is by looping until you see the correct/desired input. 一种方法是循环播放,直到看到正确/所需的输入为止。 One problem with this kind of approach is that it will never terminate if user doesn't enter the expected values (if this is what you are looking for), 这种方法的一个问题是,如果用户不输入期望值(如果您要查找的是期望值),它将永远不会终止,

Code Snippet: 代码段:

public void createStudentRecord(Scanner in) {
    String inputStudentID = null;
    int inputMark = 0;

    while (true) {
        System.out.println("Enter Student ID: ");
        inputStudentID = in.nextLine();
        if (!(inputStudentID.length() == 6)) {
            System.out.println("Enter a student ID that is 6 characters");
        } else {
            break;
        }
    }

    while (true) {
        System.out.println("Enter Module Mark: ");
        inputMark = Integer.parseInt(in.nextLine());
        if (inputMark < 0 || inputMark > 100) {
            System.out.println("PLease enter a module mark between 0-100");
        } else {
            break;
        }
    }

    addStudent(inputStudentID, inputMark);
    System.out.println("\nNew student record has been successfully created.\n");
    countRecords();
}

There are two ways I would go about solving this problem. 解决这个问题有两种方法。 The first is to loop on each variable until a valid response is given. 首先是循环访问每个变量,直到给出有效响应为止。

public void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = -1;

    while (inputStudentID == null)
    {
      System.out.println("Enter Student ID: ");
      inputStudentID = in.nextLine();
      if (inputStudentID.length() != 6)
      {
          System.out.println("Enter a student ID that is 6 characters");
          inputStudentID = null;
      }
    }

    while (inputMark == -1)
    {
        System.out.println("Enter Module Mark: ");
        inputMark = in.nextLine();
        if (inputMark < 0 || inputMark > 100) {
            System.out.println("PLease enter a module mark between 0-100");
            inputMark = -1;
        }
    }

    addStudent(inputStudentID, inputMark);
    System.out.println();
    System.out.println("New student record has been successfully created");
    System.out.println();
    countRecords();
}

The second is to use recursion. 第二个是使用递归。

public void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    inputStudentID = in.nextLine();
    if (inputStudentID.length() == 6)
    {
        System.out.println("Enter a student ID that is 6 characters");
        createStudentRecord(in);
        return;
    }

    System.out.println("Enter Module Mark: ");
    inputMark = in.nextLine();
    if (inputMark < 0 || inputMark > 100)
    {
        System.out.println("PLease enter a module mark between 0-100");
        createStudentRecord(in);
        return;
    }

    addStudent(inputStudentID, inputMark);
    System.out.println("\nNew student record has been successfully created\n");
    countRecords();
}

Change the if-else statement to while statement. 将if-else语句更改为while语句。

public static void createStudentRecord(Scanner in)
{
    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    in.nextLine();
    inputStudentID = in.nextLine();

    while (!(inputStudentID.length()==6))
    {
        System.out.println("Enter a student ID that is 6 characters");
    }

    System.out.println("Enter Module Mark: ");
    inputMark = in.nextInt();
    while (inputMark <0 || inputMark >100)
    {
        System.out.println("PLease enter a module mark between 0-100");
    }
    addStudent(inputStudentID, inputMark);
    System.out.println();
    System.out.println("New student record has been " + 
                "successfully created");
    System.out.println();
    countRecords();

}

Also, you can set a timeout variable to define the max number of times that user can try again. 另外,您可以设置超时变量以定义用户可以重试的最大次数。 Such like, 像这样

public static void createStudentRecord(Scanner in)
{
    int timeout1 = 3, timeout2 = 3;

    inputStudentID = null;
    inputMark = 0;

    System.out.println("Enter Student ID: ");
    in.nextLine();
    inputStudentID = in.nextLine();

    while (!(inputStudentID.length()==6))
    {
        System.out.println("Enter a student ID that is 6 characters");
        --timeout1;
        if( timeout1 == 0 ) {
            System.out.println("say something here");
            return;
        }
    }

    System.out.println("Enter Module Mark: ");
    inputMark = in.nextInt();
    while (inputMark <0 || inputMark >100)
    {
        System.out.println("PLease enter a module mark between 0-100");
        --timeout2;
        if( timeout2 == 0 ) {
            System.out.println("say something here");
            return;
        }
    }
    addStudent(inputStudentID, inputMark);
    System.out.println();
    System.out.println("New student record has been " + 
                "successfully created");
    System.out.println();
    countRecords();

}

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

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