我的程序允许用户重复输入匹配结果,直到用户输入“停止”,然后显示结果。我只需要在我的统计数据中找到最高的输入值[2]' arraylist,这是' hometeam得分'。因此,如果用户输入3个输入,"曼城:曼联:2:1 ... 切尔西:阿森纳:0:1 ...... 埃弗顿:利物浦:1:1"那么它应该显示数字' 2'因为它是该阵列中的最高值。如果这有道理?我已经在底部设置了我需要显示的行。
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
ArrayList<String[]> stats = new ArrayList<>(); // initialize a container
// to hold all the stats
System.out.println("please enter match results:");
int matchesPlayed = 0;
int invalidEntry = 0;
while (sc.hasNextLine()) {
String input = sc.nextLine();
String[] results = input.split("\\s*:\\s*");
if (results.length == 4) {
stats.add(results);
matchesPlayed++;
}
else if (input.equals("stop")) {
break;
}
} // end of while
for (int i = 0; i < stats.size(); i++) {
if (stats.get(i)[0].trim().isEmpty()) {
System.out
.println("no home team name entered on the following line:");
invalidEntry++;
matchesPlayed--;
}
else if (stats.get(i)[1].trim().isEmpty()) {
System.out
.println("no away team name entered on the following line:");
invalidEntry++;
matchesPlayed--;
}
else if (stats.get(i)[2].trim().isEmpty()) {
System.out.println("no home score entered on this line");
invalidEntry++;
matchesPlayed--;
}
else if (stats.get(i)[3].trim().isEmpty()) {
System.out.println("no away score entered on this line");
invalidEntry++;
matchesPlayed--;
}
try {
System.out.println(String.valueOf(stats.get(i)[0]) + " ["
+ Integer.valueOf(stats.get(i)[2]) + "] " + " | "
+ (String.valueOf(stats.get(i)[1])
+ " [" + Integer.valueOf(stats.get(i)[3]) + "] "));
} catch (Exception e) {
// do nothing with any invalid input
}
}
System.out.println(" ");
System.out.println("Totals");
System.out.println("-------------------------");
System.out.println("total number of matches: " + matchesPlayed);
System.out.println("total number of invalid entries: " + invalidEntry);
System.out.println("highest home score: ");
}
答案 0 :(得分:0)
只需循环遍历它,设置一个等于第一个元素的变量,并在循环中如果有一个元素高于当前变量,则将变量的值更改为该值。然后在末尾打印变量