Java InputMismatchException无法使用Integer.parseInt(args[0]);没有扫描仪,你怎么能让异常正常工作呢

Java InputMismatchException无法使用Integer.parseInt(args[0]);没有扫描仪,你怎么能让异常正常工作呢,java,Java,因此,如果用户将年龄输入为fish而不是整数,则InputMismatchException会打印“输入错误,请输入一个数字”。 或者,如果用户未在cmd(命令行)中输入任何内容,则ArrayIndexOutOfBoundsException会打印“命令行上的参数不足” Fuuthermore不应使用扫描仪。和age=Integer.parseInt(args[0]);必须用于将年龄从字符转换为整数 修改有问题的程序,以便在程序运行时从命令行获取用户的年龄。如果用户没有在命令行中输入数字或输入时

因此,如果用户将年龄输入为fish而不是整数,则InputMismatchException会打印“输入错误,请输入一个数字”。 或者,如果用户未在cmd(命令行)中输入任何内容,则ArrayIndexOutOfBoundsException会打印“命令行上的参数不足”

Fuuthermore不应使用扫描仪。和age=Integer.parseInt(args[0]);必须用于将年龄从字符转换为整数

修改有问题的程序,以便在程序运行时从命令行获取用户的年龄。如果用户没有在命令行中输入数字或输入时出错,您的程序应该能够处理这些问题

代码如下:

 public class age {

public static void main(String args[]){
    int age = 0;




     // try   
    try{
       // convert age from character to integer 
       age = Integer.parseInt( args [0]) ;

       // check input age value
       // if ueser inputs age less or equal to 12
        if(age<= 12)
        System.out.println("You are very young");
        // if user enters age larger than 12 and less than 20
         if(age > 12 && Age < 20)
            System.out.println("You are a teen");
        // if user enters age larger than 20
       if (age > 20)
            System.out.println("wow" +Age+" is very old");
    }
    catch(InputMismatchException e){
       System.out.println("Error in the input enter a number");
    }
 catch ( ArrayIndexOutOfBoundsException exception0) {System.out.println   ("Not enough arguments on the command line" ) ; }
  }   
}   
公共课年龄{
公共静态void main(字符串参数[]){
int年龄=0;
//试一试
试一试{
//将年龄从字符转换为整数
age=Integer.parseInt(args[0]);
//检查输入年龄值
//如果用户输入的年龄小于或等于12岁
如果(12岁和<20岁)
System.out.println(“你是青少年”);
//如果用户输入的年龄大于20岁
如果(年龄>20岁)
System.out.println(“哇”+Age+“很老”);
}
捕获(输入不匹配异常e){
System.out.println(“输入数字时出错”);
}
catch(ArrayIndexOutOfBoundsException异常0){System.out.println(“命令行上的参数不够”);}
}   
}   

因此,您希望捕获输入不是数字时引发的异常。由于您使用的是
Integer.parseInt(字符串s)
,因此抛出的异常将是。(有关详细信息,请参阅JavaDoc)

注意:您应该按照

您的代码应该如下所示:

public class Age {
    public static void main(String args[]) {
        int age = 0;

        try {
            // convert age from character to integer 
            age = Integer.parseInt(args[0]) ;
            // check input age value
            // if ueser inputs age less or equal to 12
            if(age <= 12)
                System.out.println("You are very young");
            // if user enters age larger than 12 and less than 20
            if(age > 12 && age < 20)
                System.out.println("You are a teen");
            // if user enters age larger than 20
            if (age > 20)
                System.out.println("wow " + age + " is very old");
        } catch (NumberFormatException e){
            System.out.println("Error in the input enter a number");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Not enough arguments on the command line" ); 
        }
    }
}
进入


这是重复的…当您需要第一个参数时,可能会重复您应该使用的参数
args[0]
。当你想把42岁的年龄传给程序时,你还必须执行类似java Age 42的程序。@Wufo和@Piro:这不是问题的重复。由于问题是读取参数,而不是从
系统中读取。在
@devpuh中,他已经从args[2]中读取了年龄,显然在这种情况下,问题需要更好的解释。
// if user enters age larger than 20
if (age > 20)
// if user enters age at least 20
if (age >= 20)