Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Arrays Iteration Looping Over 2d Arrays

Joshua Thao
Joshua Thao
6,438 Points

How would we have printed this out if the names were not in order of the scores?

If I wanted to print alena score for Ben what do I need to do in my code?

String[] friends = {"Ben", "Alena", "Pasan"};

    int[][] scoreCards = { // to get score of ben - scoreCards[0], if index of 1 - scoreCards[0][1] etc.
            {1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2},// ben
            {2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2},// alena
            {4, 4, 2, 1, 2, 2, 1, 4, 2, 2, 2, 3, 2, 5, 8, 1, 2, 2} // pasan score at 15th hole would be [2][14]
    };

    // for each friend print name
    for (int index = 0; index < friends.length; index++) {
        System.out.printf("%s %n-------------------------%n",
                friends[index]);
        // for each hole print score
        for (int j = 0; j < scoreCards[index].length; j++){
            System.out.printf("Hole #%d: %d %n",
                    j + 1,
                    scoreCards[index][j]);
        }
    }

1 Answer

Zhaopeng Wang
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Zhaopeng Wang
Full Stack JavaScript Techdegree Graduate 32,210 Points
  • if you want to print Alena's score before Ben, you can do a swap in scorecards
  • Also, you need to modify the String[] friends and let Alena be the first
String[] friends = {"Alena", "Ben", "Pasan"};
        int[][] scoreCards = { // to get score of ben - scoreCards[0], if index of 1 - scoreCards[0][1] etc.
                {1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2},// ben
                {2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2},// alena
                {4, 4, 2, 1, 2, 2, 1, 4, 2, 2, 2, 3, 2, 5, 8, 1, 2, 2} // pasan score at 15th hole would be [2][14]
        };

        int[] temp = new int[18];
        temp = scoreCards[0];
        scoreCards[0] = scoreCards[1];
        scoreCards[1] = temp;

        // for each friend print name
        for (int index = 0; index < friends.length; index++) {
            System.out.printf("%s %n-------------------------%n",
                    friends[index]);
            // for each hole print score
            for (int j = 0; j < scoreCards[index].length; j++) {
                System.out.printf("Hole #%d: %d %n",
                        j + 1,
                        scoreCards[index][j]);
            }
        }