【问题标题】:How to ignore string input?如何忽略字符串输入?
【发布时间】:2019-10-12 20:51:56
【问题描述】:

我有一个问题,我该如何做才能忽略所有非数字 int 的输入。

例如:

选项 1(添加) “输入两个整数:” 输入:asdf(

我相信这个验证将在 case switch 内完成,我在谷歌上搜索了一些关于解析和异常的信息,但它们并没有完全切入我正在寻找的内容。

欢迎提出建议、提示和批评。

提前致谢!

 public class MathTeacher {


  public static int addNumbers(int n1, int n2){
  int add = n1+n2;
  return add;
  }

  public static int subtractNumbers(int n1, int n2){
  int subs = n1-n2;
return subs;
  }

  public static int multiplyNumbers(int n1, int n2){
  int mulp = n1*n2;
return mulp;
  }

  public static int divideNumbers(int n1, int n2){
  int div = n1/n2;
  return div;
  }
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while(true) {
      int choices;
      System.out.println(
          "Welcome to *Mental Math Practice* where you can test your addition, subtraction, multiplication, and division.");
      System.out.println("Enter 1 to add the two numbers.");
      System.out.println("Enter 2 to subtract the second number from the first number.");
      System.out.println("Enter 3 to multiply the two numbers.");
      System.out.println("Enter 4 to divide the first number by the second number.");

      choices = scanner.nextInt();

      switch(choices){
        case 1: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int add = addNumbers(n1,n2);
          System.out.println(add);
          continue;
        }
        case 2: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int sub = subtractNumbers(n1,n2);
          System.out.println(sub);
          continue;
        }
        case 3: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int mulp = multiplyNumbers(n1,n2);
          System.out.println(mulp);
          continue;
        }
        case 4: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int div = divideNumbers(n1,n2);
          System.out.println(div);
          continue;
        }
      }
      System.out.println("Enter 'Quit' to end the program.");
    }

  }

}

【问题讨论】:

标签: java string validation input int


【解决方案1】:

如果我对问题的理解正确,您希望反复询问用户输入,直到他们提供一个整数。

下面的方法将要求用户输入,直到他们输入一个可以解析成 int 的字符串而不抛出异常。一旦提供了有效的 int,循环就会中断。

private static int getIntFromUser(Scanner scan) {
    int x = 0;
    while (true) {
        try {
            System.out.println("Enter an integer: ");
            x = scan.nextInt();
            break;
        } catch (InputMismatchException e) {
            scan.next();
        }
    }
    return x;
}

现在,检索两个数字的输入将如下所示:

int n1 = getIntFromUser(scan);
int n2 = getIntFromUser(scan);

【讨论】:

    【解决方案2】:

    如果您只需要用户输入一个整数作为输入,请尝试使用 do while 循环

    do{
        //Ask for user input here
     }while(!scanner.hasNextInt());
    

    如果输入不是整数,则此循环应继续运行。

    【讨论】:

    • 我相信如果用户输入一个非整数然后一个整数,这将失败。可以用两台扫描仪修复(和不同的条件)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多