【问题标题】:Basic I/O problems in JavaJava中的基本I/O问题
【发布时间】:2014-02-20 04:26:43
【问题描述】:

我是 java 的新手。我编写的这段代码检查整数是奇数还是偶数。当我输入一个字母时遇到了问题,错误基本上是它期待一个整数,没有别的。我的问题是,在为 i 分配 inputChar 的值之前,如何检查输入是否为有效整数?

谢谢

import java.util.Scanner;
class Oddeven {
public static void main(String[] arguments) {

  System.out.println("Type in any integer");
  Scanner inputChar = new Scanner(System.in);

  int i = inputChar.nextInt();

  if (i != 0) {
    if (i % 2 == 0) 
      System.out.println(i + " is even");
      else {
      System.out.println (i + " is odd") ;
      }
      }
   else {
   System.out.println("Zeros are not allowed, bye!");
   }
  }
 }

【问题讨论】:

  • 可能类似于if (inputChar.hasNextInt()) { ... }
  • 您可以使用 try 阻止并捕获有关无效 int 的异常

标签: java exception java.util.scanner


【解决方案1】:

你应该使用

inputChar.hasNextInt()

如果此函数返回 true,则表示下一个输入是整数。如果它返回 false 则该输入不是整数。你可以这样使用它。

if(inputChar.hasNextInt())
{
 if (i != 0) {
      if (i % 2 == 0) 
      System.out.println(i + " is even");
      else {
      System.out.println (i + " is odd") ;
      }
      }
   else {
      System.out.println("Zeros are not allowed, bye!");
   }
}
else {
     System.out.println("Other than integer inputs are not allowed, bye!");
}
}

【讨论】:

    【解决方案2】:

    您可以在调用inputChar.nextInt(); 时添加try-catch 块,并在抛出异常时打印“Not a number”(如果用户键入字母就是这种情况)。

    试试这个:

    int i = 0;  
    try
    {
        i = inputChar.nextInt();
    }
    catch (InputMismatchException e)
    {
        System.out.println("Not a number");
    }
    

    【讨论】:

    • 不要捕获一般异常
    • 这有什么坏处,不过,我已经把它改成了InputMismatchException,这就是它所抛出的。
    • 捕获一般异常可能是有害的,并且因此浪费了许多工时来跟踪已完成的错误。如果这个答案是针对经验丰富的程序员的,我不会接受。
    • 那么谢谢你的建议。我是一位经验丰富的程序员,一直使用 Exception 类作为类型,因为我不知道可能引发的实际错误是什么,并且所有异常都是该类的子类。
    【解决方案3】:
    import java.util.Scanner;
    class Oddeven {
    public static void main(String[] arguments) {
    
      System.out.println("Type in any integer");
      Scanner inputChar = new Scanner(System.in);
    
      while (true) {
          Integer i=0;
          Object test = inputChar.next();
          if(test instanceof Integer)
          {
              i=(Integer)test;
              if (i == 0) {
                System.out.println("Zeros are not allowed, try again!");
                continue;
              }
    
              System.out.println(i + " is " + ((i % 2 == 0) ? "even" : "odd"));
    
          }
          else{
              System.out.println("Please Enter An integer");
          }
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      使用 hasNextInt() 验证来自扫描仪的输入。

      import java.util.Scanner;
      
      class Oddeven {
      
          public static void main(String[] arguments) {
      
              System.out.println("Type in any integer");
              Scanner inputChar = new Scanner(System.in);
      
              int i;
      
              if(!inputChar.hasNextInt())
                  System.out.println("Not a number");
              else{
      
                  i = inputChar.nextInt();
      
                  if (i != 0) {
      
                      if (i % 2 == 0)
                          System.out.println(i + " is even");
                      else {
                          System.out.println (i + " is odd") ;
                      }
                  } else {
                      System.out.println("Zeros are not allowed, bye!");
                  }
              }
          }
      }
      

      【讨论】:

      • OP 不要求 pos 号码,所以你的例子可能会让他感到困惑。考虑简化。
      • 他只需要查看 hasNextInt() 的用法!无论如何我要修改他的代码并粘贴它。 :)
      【解决方案5】:

      好的,这是我的无限循环解决方案,当用户输入 0 或终止程序时会中断:

      import java.util.Scanner;
      
      class Oddeven {
      
          public static void main(String[] arguments) {
      
              Scanner inputChar = new Scanner(System.in);
      
              int i;
      
              while (true) {
      
                  System.out.println("Type in any integer:");
      
                  if(!inputChar.hasNextInt()) {
      
                      System.out.println("Not a number");
                      inputChar.next();
      
                  } else {
      
                      i = inputChar.nextInt();
      
                      if (i != 0) {
      
                          if (i % 2 == 0)
                              System.out.println(i + " is even");
      
                          else {
                              System.out.println (i + " is odd") ;
                          }
      
                      } else {
      
                          System.out.println("Zeros are not allowed, bye!");
                          break;
      
                      }
                  }
              }
          }
      }
      

      有不明白的地方可以问,希望对你有帮助,加油!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-07
        • 2010-10-14
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-24
        相关资源
        最近更新 更多