我偶然发现了这个问题:
编写一个完整的Java程序command.java,该程序在单独的一行上以相反的顺序打印出所有命令行参数。它可以有任意多个命令行参数。例如,java command az ba cad将输出
dac
ab
za我并不是想要一些填鸭式的答案,但以下是我想出来的一些想法:
1)一旦我将每一行都赋值到一个字符串变量中,就可以通过StringBuilder来反转字符串
然而,我面临的问题是,我是否能够遍历“命令行”本身,因为它要求“任意多个命令行”,这意味着我不能使用for循环,因为我不知道范围,但我可以使用while循环吗?如果是这样的话,是怎么做的?谢谢。
发布于 2016-04-21 13:36:30
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]);
}
}发布于 2016-05-20 13:14:09
class Command {
public static void main(String args[]){
for(int i= args.length-1; i>=0; i--){
StringBuilder input = new StringBuilder();
input.append(args[i]);
System.out.println(input.reverse());
}
}
}https://stackoverflow.com/questions/36760457
复制相似问题