ind*_*da1 0 java arrays string
我需要从命令行中取出单词,将它们保存到一个数组中,然后打印出如下的单词:
input: asdf jkl qwerty dfs
output: - jkl qwerty dfs
asdf - qwerty dfs
asdf jkl - qwerty dfs
asdf jkl qwerty -
Run Code Online (Sandbox Code Playgroud)
此外,如果用户只提供2个单词,我应该会得到相同的结果.当所给出的论据数量每次都不同时,我不明白我会怎么做.这是我尝试过的:
public static void main(String[] args)
{
String input1 = args[0];
String input2 = args[1];
String input3 = args[2];
String input4 = args[3];
String[] input = new String[4];
}
public static void printExceptOne(String[] exception, int x)
{
System.out.print("-");
System.out.print(" "+exception[1]);
System.out.print(" "+exception[2]);
System.out.println(" "+exception[3]);
}
}
Run Code Online (Sandbox Code Playgroud)
public class Solution {
public static void main(String[] args) {
printExceptOne(args);
}
private static void printExceptOne(String[] args) {
for (int i = 0; i < args.length; i++) {
for (int j = 0; j < args.length; j++) {
String output = j == i ? "-" : args[j];
// adjust spaces however you like
System.out.print(" " + output);
}
System.out.println();
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际测试
输入
asdf jkl qwerty dfs
Run Code Online (Sandbox Code Playgroud)
产量
- jkl qwerty dfs
asdf - qwerty dfs
asdf jkl - dfs
asdf jkl qwerty -
Run Code Online (Sandbox Code Playgroud)
注意:我假设您预期输出的第3行不正确.你有它
[asdf jkl - qwerty dfs]
Run Code Online (Sandbox Code Playgroud)