如何使用 System.out 格式化输出?
How can I format the output with System.out?
我在业余时间制作英超足球 table,但遇到了一个问题。当程序运行时我希望它是完美的并以我想要的格式输出,问题是:
您按如下方式输入 Input (" HomeTeam : AwayTeam : HomeScore : AwayScore ")
完成列表后,输入 "quit" 停止程序
我的问题是分数是这样出来的
(" HomeTeam | AwayTeam | HomeScore | AwayScore ")
我打算这样打印 (" HomeTeam [HomeScore] | AwayTeam [AwayScore] ")
我已经尝试了 System.out.printlns 的许多变体,但都无济于事,甚至尝试制作几个布尔条件,以按照我想要的方式输出输入。我真的很茫然,很沮丧 - 我希望有人能给我提示代码已附上
编辑循环;
for (int i = 0; i < counter; i++) { // A loop
String[] words = product_list[i].split(":");
System.out.println(words[0].trim() + "[" + words[2].trim() + "]" + " | " + words[1].trim() + "[" + words[3].trim()) + "]";
这应该有效:
Scanner sc = new Scanner(System.in);
public void outputScore(String input) {
String[] words = input.trim().split("\s+");
String satisfied = sc.nextLine();
if (satisfied.equals("quit")) {
System.out.println(words[0] + " [" + words[4] + "] | " + words[2] + " [" + words[6] + "]");
}
}
当您调用该方法时,它应该是这样的:
outputScore(sc.nextLine());
这是您编辑的问题的代码:
String [] product_list = new String [100];
int counter = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Input as follows:");
System.out.println("Home team : Away team : Home score : Away score");
String line = null;
while (!(line = scanner.nextLine()).equals("")) {
if (line.equals("quit")) {
break;
} else {
product_list[counter] = line;
System.out.println("Home team : Away team : Home score : Away score");
}
counter++;
}
for (int i = 0; i < counter; i++) {
String[] words = product_list[i].split(":");
System.out.println(words[0].trim() + " : " + words[2].trim() + " | " + words[1].trim() + " : " + words[3].trim());
}
希望对您有所帮助。
我在业余时间制作英超足球 table,但遇到了一个问题。当程序运行时我希望它是完美的并以我想要的格式输出,问题是:
您按如下方式输入 Input (" HomeTeam : AwayTeam : HomeScore : AwayScore ")
完成列表后,输入 "quit" 停止程序
我的问题是分数是这样出来的 (" HomeTeam | AwayTeam | HomeScore | AwayScore ")
我打算这样打印 (" HomeTeam [HomeScore] | AwayTeam [AwayScore] ")
我已经尝试了 System.out.printlns 的许多变体,但都无济于事,甚至尝试制作几个布尔条件,以按照我想要的方式输出输入。我真的很茫然,很沮丧 - 我希望有人能给我提示代码已附上
编辑循环;
for (int i = 0; i < counter; i++) { // A loop
String[] words = product_list[i].split(":");
System.out.println(words[0].trim() + "[" + words[2].trim() + "]" + " | " + words[1].trim() + "[" + words[3].trim()) + "]";
这应该有效:
Scanner sc = new Scanner(System.in);
public void outputScore(String input) {
String[] words = input.trim().split("\s+");
String satisfied = sc.nextLine();
if (satisfied.equals("quit")) {
System.out.println(words[0] + " [" + words[4] + "] | " + words[2] + " [" + words[6] + "]");
}
}
当您调用该方法时,它应该是这样的:
outputScore(sc.nextLine());
这是您编辑的问题的代码:
String [] product_list = new String [100];
int counter = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Input as follows:");
System.out.println("Home team : Away team : Home score : Away score");
String line = null;
while (!(line = scanner.nextLine()).equals("")) {
if (line.equals("quit")) {
break;
} else {
product_list[counter] = line;
System.out.println("Home team : Away team : Home score : Away score");
}
counter++;
}
for (int i = 0; i < counter; i++) {
String[] words = product_list[i].split(":");
System.out.println(words[0].trim() + " : " + words[2].trim() + " | " + words[1].trim() + " : " + words[3].trim());
}
希望对您有所帮助。