[英]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.我希望我的二维数组实际输出为普通的 5x5 网格,而不是水平线或垂直线,这是我目前的代码。 (I'm using String for the numbers because I need to later replace them with "XX")
(我使用 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();
}
}
}
}
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.基本上希望网格是对称的而不是全部随机的,如果可能的话,我打算使用 \\t ,如果不是,那么我会接受其他不太复杂的解决方案。 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]) System.out.println(card1[i][j])
With和
System.out.print(card1[i][j]) System.out.print(card1[i][j])
And和
Move this line one step down (after loop of j completed)将此行向下移动一步(在 j 循环完成后)
System.out.println() System.out.println()
For space use 01 instead of 1 or put manual condition to check value less then 10 then modify it accordingly对于空间使用 01 而不是 1 或手动条件检查值小于 10 然后相应地修改它
Looks like you just need to learn to use System.out.print
in place of System.out.println
.看起来您只需要学习使用
System.out.print
代替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.您可能还希望使用
System.out.printf
将每个字符串打印为两个字符。
See the documentation for further details.有关更多详细信息,请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.