该程序应该存储2个团队名称和2个分数的输入,这样做后应输出它们。
数组只是打印“null [null] | null [null]”
import java.util.Scanner;
public class C3484209 {
public static void main(String[] args) {
String[] home_team_name = new String[100];
String[] away_team_name = new String[100];
String[] home_team_score = new String[100];
String[] away_team_score = new String[100];
Scanner keyboard = new Scanner(System.in);
String line = "";
while (!(line.equalsIgnoreCase("stop")))
{
System.out.println("Enter the teams that played and the score :");
System.out.println("(Please note that the standard format is Home team : Away Team : Home Score : Away Score)");
line = keyboard.nextLine();
String[] elements = line.split(": ");
for ( int i = 0; i < elements.length; )
{
if ((int)i == 0)
{
home_team_name[i] = elements[0];
i++;
}
else if ((int)i == 1)
{
away_team_name[i] = elements[1];
i++;
}
else if ((int)i == 2)
{
home_team_score[i] = elements[2];
i++;
}
else if((int)i == 3)
{
away_team_score[i] = elements[3];
i++;
}
//System.out.print("Element " + (i+1) + " was : " + elements[i] + " | ");
System.out.println(home_team_name[i] + " [" + home_team_score[i] + "]" + " | " + away_team_name[i] +" [" + away_team_score[i] + "]");
}
}
//Output Command
keyboard.close();
}
}
答案 0 :(得分:0)
试试这个!
package cn.sehzh;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] home_team_name = new String[100];
String[] away_team_name = new String[100];
String[] home_team_score = new String[100];
String[] away_team_score = new String[100];
Scanner keyboard = new Scanner(System.in);
String line = "";
int j = 0;
while (!(line.equalsIgnoreCase("stop"))) {
System.out.println("Enter the teams that played and the score :");
System.out.println(
"(Please note that the standard format is Home team : Away Team : Home Score : Away Score)");
line = keyboard.nextLine();
String[] elements = line.split(": ");
for (int i = 0; i < elements.length;) {
if ((int) i == 0) {
home_team_name[j] = elements[0];
i++;
} else if ((int) i == 1) {
away_team_name[j] = elements[1];
i++;
} else if ((int) i == 2) {
home_team_score[j] = elements[2];
i++;
} else if ((int) i == 3) {
away_team_score[j] = elements[3];
i++;
}
}
// System.out.print("Element " + (i+1) + " was : " + elements[i]
// + " | ");
System.out.println(home_team_name[j] + " [" + home_team_score[j] + "]" + " | " + away_team_name[j] + " ["
+ away_team_score[j] + "]");
j++;
}
// Output Command
keyboard.close();
}
}
你应该学习如何调试!
答案 1 :(得分:0)
你只是犯了一个简单的错误。您正在为String []数组保存值,索引i应该是反向的
if ((int) i == 0) {
home_team_name[k] = elements[i];
}
数组从索引0开始,您将在不同的索引处保存值
你应该试试这个!
package stackoverflow;
import java.util.Scanner;
public class C3484209 {
public static void main(String[] args) {
String[] home_team_name = new String[100];
String[] away_team_name = new String[100];
String[] home_team_score = new String[100];
String[] away_team_score = new String[100];
Scanner keyboard = new Scanner(System.in);
String line = "";
int k = 0;
while (!(line.equalsIgnoreCase("stop"))) {
System.out.println("Enter the teams that played and the score :");
System.out.println(
"(Please note that the standard format is Home team : Away Team : Home Score : Away Score)");
line = keyboard.nextLine();
String[] elements = line.split(": ");
for (int i = 0; i < elements.length; i++) {
if ((int) i == 0) {
home_team_name[k] = elements[i];
} else if ((int) i == 1) {
away_team_name[k] = elements[i];
} else if ((int) i == 2) {
home_team_score[k] = elements[i];
} else if ((int) i == 3) {
away_team_score[k] = elements[i];
}
}
System.out.println(home_team_name[k] + " [" + home_team_score[k] + "]" + " | " + away_team_name[k] + " ["
+ away_team_score[k] + "]");
k++;
}
keyboard.close();
}
}