簡體   English   中英

如何在 Java 中為 5x5 二維數組正確使用制表符空間?

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

我希望我的二維數組實際輸出為普通的 5x5 網格,而不是水平線或垂直線,這是我目前的代碼。 (我使用 String 作為數字,因為我稍后需要用“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();
            }
        }

    }
}

當前輸出為:

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

我希望輸出是什么:

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

基本上希望網格是對稱的而不是全部隨機的,如果可能的話,我打算使用 \\t ,如果不是,那么我會接受其他不太復雜的解決方案。 謝謝,麻煩您了。

嘗試這個

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

嘗試這個。

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();
}

輸出:

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 

代替

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

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

將此行向下移動一步(在 j 循環完成后)

System.out.println()

對於空間使用 01 而不是 1 或手動條件檢查值小於 10 然后相應地修改它

看起來您只需要學習使用System.out.print代替System.out.println 這樣做將不使用換行符。 您可能還希望使用System.out.printf將每個字符串打印為兩個字符。

有關更多詳細信息,請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM