如何在数组索引的每个数字上显示XXX|XXX? – Java

huangapple go评论78阅读模式
英文:

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(&quot;&quot;);
	for(int i = 0; i&lt;4; i++)
	{
		//System.out.println(i);
		for(int j = 0; j &lt; numbers[i];j++)
		{
			//System.out.println(j);
			
			System.out.print(&quot; XXX|XXX &quot;);
			
			
			
		}
		
		System.out.println(&quot; &quot;);
		
	}

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

通过使用 arrayString.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:

{&quot;       &quot;,&quot;       &quot;,&quot;       &quot;,&quot;XXX|XXX&quot;}
{&quot;       &quot;,&quot;       &quot;,&quot;XXX|XXX&quot;,&quot;XXX|XXX&quot;}
{&quot;       &quot;,&quot;XXX|XXX&quot;,&quot;XXX|XXX&quot;,&quot;XXX|XXX&quot;}
{&quot;XXX|XXX&quot;,&quot;XXX|XXX&quot;,&quot;XXX|XXX&quot;,&quot;XXX|XXX&quot;}

int [] board = {1,2,3,4};

for (int i : board) {
	String[] pile = new String[board[board.length-1]];
	for (int x = 0; x &lt; pile.length; x++) {
		if (x &lt; pile.length-i) {
			pile[x] = &quot;       &quot;;
		} else {
			pile[x] = &quot;XXX|XXX&quot;;
		}
	}
	String s = String.join(&quot; &quot;, 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 &lt; board.length; i++) {
            for (int j = 0; j &lt; board[i]; j++) {
                System.out.print(&quot;XXX|XXX &quot;);
            }
            System.out.println(&quot;&quot;);
            
        }
    }

huangapple
  • 本文由 发表于 2020年10月15日 06:55:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64362585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定