简体   繁体   中英

How to properly use tab spaces in Java for a 5x5 2D array?

I want my 2D array to actually output as a normal 5x5 grid rather than in a horizontal line or a vertical line, this is my code so far. (I'm using String for the numbers because I need to later replace them with "XX")

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class assignMain {

    // Global declarations.
    public static Scanner choice = new Scanner(System.in);
    public static String player1 = "Player 1's card:";
    public static String player2 = "Player 2's card:";
    public static int i;
    public static int j;

    public static void main (String[] args) {
        // Declare list to contain used numbers.
        List<Scanner> usedNums = new ArrayList<>();
        usedNums.add(choice);

        // Declare card 1 numbers
        String[][] card1 = { {"24", "2", "8", "1", "25"},
                            {"12", "16", "7", "17", "15"},
                            {"5", "6", "20", "19", "13" },
                            {"14", "23", "22", "4", "3" },
                            {"10", "18", "11", "21", "9"} };
        // Declare card 2 numbers
        String[][] card2 = { {"24", "21", "17", "15", "6"},
                            {"10", "3", "8", "18", "20"  },
                            {"14", "7", "16", "12", "5"  },
                            {"25", "23", "13", "19", "11"},
                            {"22", "4", "9", "1", "2"    } };
        printCard(card1, card2);
    }

    public static void printCard(String[][] card1, String[][] card2) {
        System.out.println(player1);
        for(i = 0; i < card1.length; i++) {
            for(j = 0; j < card1.length; j++) {
                System.out.println(card1[i][j]);
                System.out.println();
            }
        }

    }
}

The current output is :

Player 1's card:
24

2

8

1

25

12

16

7

17

15

5

6

20

19

13

14

23

22

4

3

10

18

11

21

9

What I'd like the output to be :

Player 1's card:
24  2  8  1 25
12 16  7 17 15
 5  6 20 19 13
14 23 22  4  3
10 18 11 21  9

Basically want the grid to be symmetric and not all random, I intend to use \\t if that's possible, if not then I'll be ok with other solutions which aren't too complicated. Please and thank you.

Try this

String[][] card1 = { {"24", "2", "8", "1", "25"},
                {"12", "16", "7", "17", "15"},
                {"5", "6", "20", "19", "13" },
                {"14", "23", "22", "4", "3" },
                {"10", "18", "11", "21", "9"} };

        for (int i = 0; i < card1.length; i++) {
            for (int j = 0; j < card1.length; j++) {
                    System.out.print(card1[i][j]);
                System.out.print(" "); // you can replace this with "\t"
            }
            System.out.print("\n");
        }

Try this.

String[][] card1 = {
    {"24", "2", "8", "1", "25"},
    {"12", "16", "7", "17", "15"},
    {"5", "6", "20", "19", "13"},
    {"14", "23", "22", "4", "3"},
    {"10", "18", "11", "21", "9"}};

for (String[] row : card1) {
    for (String e : row)
        System.out.printf("%2s ", e);
    System.out.println();
}

output:

24  2  8  1 25 
12 16  7 17 15 
 5  6 20 19 13 
14 23 22  4  3 
10 18 11 21  9 

Replace

System.out.println(card1[i][j])

With

System.out.print(card1[i][j])

And

Move this line one step down (after loop of j completed)

System.out.println()

For space use 01 instead of 1 or put manual condition to check value less then 10 then modify it accordingly

Looks like you just need to learn to use System.out.print in place of System.out.println . Doing this will leave off the newline. You may also wish to use System.out.printf to print each string as two characters.

See the documentation for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM