gpt4 book ai didi

Java 循环通过命令行参数来查看用户输入的内容?

转载 作者:行者123 更新时间:2023-12-01 07:51:25 25 4
gpt4 key购买 nike

我偶然发现了这个问题:

编写一个完整的 Java 程序 command.java,以相反的顺序打印出所有命令行参数,每个参数在单独的行上。它可以有任意多个命令行参数。例如,java 命令 az ba cad 将输出

dac
ab
za

我并不是在寻求填鸭式的答案,但这里有一些我想出的想法:

1)一旦我将每一行分配给一个字符串变量,就可以通过 StringBuilder 来反转字符串

但是,我面临的问题是我是否能够循环遍历“命令行”本身,因为它要求“任意多个命令行”,这意味着我无法使用 For 循环,因为我不知道范围,但我可以使用 while 循环吗?如果是这样,怎么办?谢谢。

最佳答案

public static void main(String args[]){
//here args array hold all the string inputs given from command line. you don't need
//worry how many were entered it will all be in the array.
// start printing the array from the last to first
for(int i= args.length-1; i>=0; i++){
System.out.println(args[i]);
}
}

关于Java 循环通过命令行参数来查看用户输入的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760457/

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