在ArrayList元素中查找最大/分钟输入



我的程序允许用户重复输入匹配结果,直到用户输入"停止",然后显示结果。我只需要在"统计信息[2]'arraylist,这是"祖先分数"中找到最高的输入值。因此,如果用户输入3个输入,"曼城:曼联:2:1 ...切尔西:阿森纳:0:1 ...埃弗顿:利物浦:1:1"然后它应该显示数字'2',因为它是该数组中的最高值。如果这很有意义?我已经在需要显示的地方设置了底部的行。<</p>

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: ");
}

只需循环浏览它,设置一个等于第一个元素的变量,在循环中,如果有一个元素高于当前的变量,则将变量的值更改为此。然后在末端打印变量

最新更新