如何从数组中使用的数据中打印出信息?

时间:2017-12-03 22:06:18

标签: java arrays

我的代码要求用户输入6个不同运动队在整个赛季中获得的胜利,失利和关系。我怎样才能做到这一点,一旦收到所有信息,它将打印出每个团队获得多少胜利,关系和损失,以及显示每个团队的总金额?

代码:

package SMKTeamStandings;

import java.util.Scanner;

public class SMKTeamStandings {

public static Scanner in = new Scanner(System.in);

public static int number(int max, int min) {
    int teamchoice = 0;
    for (boolean valid = false; valid == false;) {
        teamchoice = in.nextInt();
        if (teamchoice >= min && teamchoice <= max) {
            valid = true;
        } else {
            System.out.println("Please enter a different value.");
        }
    }
    return teamchoice;
}

public static boolean finished(boolean[] completedArray) {
    int i = 0;
    boolean done;
    for (done = true; done == true;) {
        if (completedArray[i++] == false) {
            done = false;
        }
    }
    return done;
}

public static void main(String[] args) {

    int teamChoice = 0, gamesNum;

    String[] sportteams = {"Basketball", "Football",
       "Hockey", "Rugby",
       "Soccer", "Volleyball"};


    boolean[] completed = new boolean[sportteams.length];
    int[][] Outcome = new int[64][sportteams.length];

    for (boolean done = false; done == false;) {
        for (int i = 0; i < sportteams.length; i++) {
            System.out.print(i + 1 + " - " + sportteams[i]);
            if (completed[i] == true) {
                System.out.println(" - Finished");
            } else {
                System.out.println();
            }
        }

        System.out.print("\nChoose a team from the list above:");

        teamChoice = number(6, 1);
        teamChoice--;

        System.out.print("\nHow many games total did the " + sportteams[teamChoice]
                + " team play this season?: ");

        gamesNum = in.nextInt();

        System.out.format("\n %10s %10s %10s %10s %10s \n\n", "", "Possible Outcomes:",
                "1 - Win",
                "2 - Tie",
                "3 - Loss");

        for (int wintieloss = 0; wintieloss < gamesNum; wintieloss++) {
            System.out.print("\nEnter the outcome for game "
                    + (wintieloss + 1) + ": ");
            Outcome[wintieloss][teamChoice] = number(3, 1);
        }
        System.out.println("\n");
        completed[teamChoice] = true;
        done = finished(completed);

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您只想输出您从用户那里获得的数据。为此,您可以使用for循环遍历数据数组并使用索引访问数据。

for(int team = 0; team < sportteams.length; team++) { // for each team
    System.out.println((team + 1) + " - " + sportteams[team]); // output the team
    int game = 0; // index of the current game
    while(Outcome[game][team] != 0) { // while there is data
        System.out.print("Game " + (game + 1) ": " + Outcome[game][team] + " "); // print the data
        game++; // increment the index
    }
    System.out.println("Total games: " + game); // print the last index == total number of games
    System.out.println();
}