英文:
How to display XXX|XXX for every number in an index of an array? - Java
问题
System.out.print(" XXX|XXX\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < numbers[i]; j++) {
System.out.print(" XXX|XXX ");
}
System.out.print("\n");
}
英文:
So I have the NIM Game Program. Basically, there are four piles and the two players have to keep taking stones from the piles until one of them lose. Right now my piles look like this in the output.
Board: [1, 2, 3, 4]
but I need to modify it so it will look like this
XXX|XXX
XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX XXX|XXX
This is my code
System.out.print("");
for(int i = 0; i<4; i++)
{
//System.out.println(i);
for(int j = 0; j < numbers[i];j++)
{
//System.out.println(j);
System.out.print(" XXX|XXX ");
}
System.out.println(" ");
}
it prints this
XXX|XXX
XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX XXX|XXX
suggestons?
答案1
得分: 1
通过使用 array
和 String.join()
方法,您可以创建一个数组,其长度为板块数组中的最后一个数字。这确保输出中包含的所有字符串的长度相同 - 没有丑陋的意外单字符偏移。然后,您可以循环遍历小于牌组数组长度的数字,并检查该位置应该是空格还是填充为 "XXX|XXX"。
然后,您将会有如下数组:
{ " ", " ", " ", "XXX|XXX" }
{ " ", " ", "XXX|XXX", "XXX|XXX" }
{ " ", "XXX|XXX", "XXX|XXX", "XXX|XXX" }
{ "XXX|XXX", "XXX|XXX", "XXX|XXX", "XXX|XXX" }
int [] board = {1,2,3,4};
for (int i : board) {
String[] pile = new String[board[board.length-1]];
for (int x = 0; x < pile.length; x++) {
if (x < pile.length-i) {
pile[x] = " ";
} else {
pile[x] = "XXX|XXX";
}
}
String s = String.join(" ", pile);
System.out.println(s);
}
输出结果:
XXX|XXX
XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX XXX|XXX
这个循环还能够支持任意长度的板块,只要板块中的每个项目递增1。
英文:
By making use of an array
and the String.join()
method, you can create an array that has a length of the last number in the board array. This ensures that all of the strings included in the output will be of the same length - no ugly, accidental single-character offsets. Then, you can loop through the numbers that are less than the length of the pile array and check to see if that position should be blank spaces or filled with "XXX|XXX"
Afterwards, you would have arrays that look like this:
{" "," "," ","XXX|XXX"}
{" "," ","XXX|XXX","XXX|XXX"}
{" ","XXX|XXX","XXX|XXX","XXX|XXX"}
{"XXX|XXX","XXX|XXX","XXX|XXX","XXX|XXX"}
int [] board = {1,2,3,4};
for (int i : board) {
String[] pile = new String[board[board.length-1]];
for (int x = 0; x < pile.length; x++) {
if (x < pile.length-i) {
pile[x] = " ";
} else {
pile[x] = "XXX|XXX";
}
}
String s = String.join(" ", pile);
System.out.println(s);
}
Output:
XXX|XXX
XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX XXX|XXX
This loop is also capable of supporting a board of any length, so long as each item in the board increases by 1.
答案2
得分: 0
以下是翻译好的代码部分:
public static void main(String[] args) {
int[] board = {1, 2, 3, 4};
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i]; j++) {
System.out.print("XXX|XXX ");
}
System.out.println("");
}
}
英文:
something like this can help you.
public static void main(String[] args) {
int [] board={1,2,3,4};
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i]; j++) {
System.out.print("XXX|XXX ");
}
System.out.println("");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论