public static void main(String[] args) {
if (args.length == 0) { //If nothing is typed into the command line, the message below is printed
System.out.println("(!)You have not entered anything in the commandline.");
}else if (args.length > 0){
try {
Integer x = new Integer(0);
if (x<=0){
System.out.println("(!)You may not have a negative number in the command line.");
}
x = Integer.parseInt(args[0]);
} catch (NumberFormatException y) {
System.out.println("(!)Your entry in the commandline must be an integer.");
System.exit(1);
}
}
}
在上面的代码中,我尝试在命令行中输入一个正号码,但它一直在运行我的println:“(!)你可能没有负数”,即使x(我输入命令行的内容)不是&lt; = 0.我有一种感觉,我忘了添加一些东西。
答案 0 :(得分:3)
Integer x = new Integer(0);
if (x<=0){
System.out.println("(!)You may not have a negative number in the command line.");
}
x = Integer.parseInt(args[0]);
这里还有什么可以期待的,你正在用零初始化并立即检查if条件。
您必须先将arg[0]
解析为整数,然后再将逻辑解析为
你必须写
Integer x = Integer.parseInt(args[0]);
if (x<=0){
System.out.println("(!)You may not have a negative number in the command line.");
}