如何在一个块中重复错误输入而不必使用嵌套循环?

时间:2015-05-29 03:55:14

标签: java while-loop nested try-catch repeat

是否可以在一个块中重复多个try-catch语句而不创建另一个(嵌套)while循环或者必须再次重复整个循环?

例如:

while(true) {
    //try-catch 1: if the user input is correct continue, or ask him to repeat this input
    //try-catch 2: if the user input is correct continue, or ask him to repeat this input
    break; //after it's done
}

另一个例子:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Example {

    public static void main(String[] args) {
        while (true) {
            Scanner scan1 = new Scanner(System.in);
            Scanner scan2 = new Scanner(System.in);
            try {
                System.out.print("Enter a number: ");
                scan1.next();
            } catch (InputMismatchException e) {
                //do something to repeat this task
            }

            try {
                System.out.println("Enter another number: ");
                scan2.next();
            } catch (InputMismatchException e) {
                //do something to repeat this task
            }
            break;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您描述的流程不需要嵌套循环,但它需要两个独立的循环。

我想你想要这样的东西:

Scanner scan1 = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);

while (true) {
    try {
        System.out.print("Enter a number: ");
        scan1.next();
        break;
    } catch (InputMismatchException e) {            
        System.out.println("Error! That wasn't a number!");
        // didn't reach a break statement, will repeat.
    }
}

while (true) {
    try {
        System.out.print("Enter another number: ");
        scan2.next();
        break;
    } catch (InputMismatchException e) {            
        System.out.println("Error! That wasn't a number!");
        // didn't reach a break statement, will repeat.
    }
}

请注意,这是很多重复的代码,因此您可以通过提取要调用的方法来改进它 - 例如:

private static int getNumber(String prompt, Scanner scan) {
    while (true) {
        try {
            System.out.print(prompt);
            return scan.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Error! Try again.");
        }
    }
}

public static void main(String[] args) {
    Scanner scan1 = new Scanner(System.in);
    Scanner scan2 = new Scanner(System.in);

    int num1 = getNumber("Enter a number: ", scan1);
    int num1 = getNumber("Enter another number: ", scan2);
}

免责声明:我的Java生锈了,所以上面的代码可能不完全正确,但我希望你明白这一点。

答案 1 :(得分:-1)

这是一个简单的例子,除非提供有效的数字,否则应用程序将不会继续。

public class TestInput
{

  public static boolean validInput(Scanner scan)
  {
    System.out.print("Enter a number: ");
    try
    {
      String stringNumber = scan.next();
      try
      {
        int myInt = Integer.parseInt(stringNumber);
      }
      catch (NumberFormatException e)
      {
        System.out.println("No I meant a Number; " + stringNumber + "  is not a number");
        return false;
      }
    }
    catch (InputMismatchException e)
    {
      return false;
    }
    return true;
  }


  public void doTheWork()
  {
    Scanner scan = new Scanner(System.in);
    if (!validInput(scan))
    {
      doTheWork();
    }
    System.out.println("All Good");
  }

  public static void main(String[] args)
  {
    TestInput testInput = new TestInput();
    testInput.doTheWork();
  }
}

这是一个重用方法来获取多个输入的简单示例

public class TestInput
{
  public static int getNumberInput(String message)
  {
    Scanner scan = new Scanner(System.in);
    try
    {
      System.out.print(message);
      String stringNumber = scan.next();
      try
      {
        return  Integer.parseInt(stringNumber);
      }
      catch (NumberFormatException e)
      {
        return getNumberInput("No I meant a Number; " + stringNumber + "  is not a number");
      }

    }
    catch (InputMismatchException e)
    {
      //do something to repeat this task
    }
    return getNumberInput(message);
  }

  public void doTheWork()
  {
    int oneParam = getNumberInput("Enter a number: ");
    int twoParam = getNumberInput("Enter another number: ");

    System.out.println("Your first input is " + oneParam);
    System.out.println("Your second input is " + twoParam);
  }

  public static void main(String[] args)
  {
    TestInput testInput = new TestInput();
    testInput.doTheWork();
  }
}