gpt4 book ai didi

java - 命令行参数

转载 作者:行者123 更新时间:2023-11-30 08:09:14 25 4
gpt4 key购买 nike

 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: "(!)You may not have a negative number"即使 x(我在命令行中输入的内容)不是<= 0。我感觉我忘了添加一些东西。

最佳答案

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

关于java - 命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32605580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com