Java中的基本I/O问题

Java中的基本I/O问题,java,exception,java.util.scanner,Java,Exception,Java.util.scanner,我是java的新手。我写的这段代码检查整数是奇数还是偶数。当我输入一封信时遇到了问题,错误基本上是它应该是一个整数,而不是别的。我的问题是,在为i分配inputChar的值之前,如何检查输入是否是有效的整数而不是其他值 谢谢 import java.util.Scanner; class Oddeven { public static void main(String[] arguments) { System.out.println("Type in any integer"); S

我是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!");
   }
  }
 }

调用
inputChar.nextInt()时,可以为添加
try catch
并在引发异常时打印“非数字”(用户键入字母时就是这种情况)

试试这个:

int i = 0;  
try
{
    i = inputChar.nextInt();
}
catch (InputMismatchException e)
{
    System.out.println("Not a number");
}
使用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!");
            }
        }
    }
}

这是我的无限循环解决方案,当用户输入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;

                }
            }
        }
    }
}
如果你不懂什么,问一下,希望能有帮助,干杯

您应该使用

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!");
}
}

可能类似于
if(inputChar.hasnetint()){…}
您可以使用
try
阻止并捕获关于无效int的异常Don't catch generic exception这有什么害处,但是,我已经将其更改为
inputmaschException
,捕获泛型异常可能是有害的,因此许多工时被浪费在跟踪已完成的错误上。如果这个答案是给一个有经验的程序员的,我就不会学了。那么谢谢你的建议。我是一名经验丰富的程序员,一直使用
异常
类作为类型,因为我不知道可能抛出的实际错误是什么,而且所有异常都是该类的子类。看看OP,它不是在询问pos编号,所以你的示例可能会让他感到困惑。考虑简化,他只需要查找HasnExtTin()用法!不管怎样,我都要修改他的代码并粘贴它。:)
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!");
}
}