Java:值的数组星号打印

时间:2016-11-10 21:06:30

标签: java arrays for-loop iteration

我正在尝试根据掷骰子的次数(通过数组)打印星号。我遇到了在星号之前打印骰子方面(i)的问题。此外,我从不知名的地方得到两个零,并且不知道它们来自哪里。 我很感激你的帮助。

我的代码:

public class Histogram {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int numRoles = 100;
        int[] amountRoles = new int[7]; // amountRoles Holds the array

        for (int i = 1; i < 7; i++) 
        amountRoles[i] = 0; // Set 0
        {
            for (int i = 0; i < numRoles; i++) 
            {
                int die1 = (int)(Math.random()*6+1);
                amountRoles[die1]++; // Increments 
            }
            System.out.println("The die was rolled " + numRoles + " times, its six value's counts are:");
            for (int i = 1; i < 7; i++)
            {
                System.out.print("Side " + i + " was rolled " + amountRoles[i]+ " times out of " + numRoles + ".");
                // Prints each sides value (i) alongside with how many times it was rolled (amountRoles[i]).
                System.out.println(); // Formatting Line
            }
        }
        for (int i = 0; i < amountRoles.length; i++) // Iterates through amountRoles
        {
            for(int j = 0; j < amountRoles[i]; j++) // Loop through amountRoles[i]
            {
                System.out.print("" + "*");
            }
            System.out.println(i + " "  + amountRoles[i]);
        }
    }
}

我的输出:

The die was rolled 100 times, its six value's counts are:
Side 1 was rolled 11 times out of 100.
Side 2 was rolled 19 times out of 100.
Side 3 was rolled 19 times out of 100.
Side 4 was rolled 17 times out of 100.
Side 5 was rolled 16 times out of 100.
Side 6 was rolled 18 times out of 100.
0 0 (Where are these zeroes coming from?)
***********1 11
*******************2 19
*******************3 19
*****************4 17
****************5 16
******************6 18

我的目标输出示例:

[1]     ******************* 19
[2]     ************ 12
[3]     ********************* 21
[4]     ******************** 20
[5]     ************* 13
[6]     *************** 15

2 个答案:

答案 0 :(得分:2)

你从零开始?

也适用于中线

System.out.println(); // Formatting Line 

以上内容可以删除,只需将ln添加到上一个输出中即可。

我还会使用变量来保持字符串的计数,然后追加它。

for (int i = 1; i < amountRoles.length; i++) // Iterates through amountRoles
    {
        String asterCount = "";
        for(int j = 0; j < amountRoles[i]; j++) // Loop through amountRoles[i]
        {
            asterCount += ("*");
        }
        System.out.println("[" +i +"]"+ "   "+ asterCount  + " "+ amountRoles[i]);
    }

答案 1 :(得分:1)

只有回答您的问题,我在代码中看到的其他问题,您才能从0 0获得int i = 0。将此更改为int i = 1,不再是0 0

要让您的值以您需要的方式打印,您只需在循环上方的*之前简单地移动您想要打印的内容。在这种情况下[i],并移动每个卷的数量低于*循环。代码如下。这应该解决你陈述的问题。

for (int i = 1; i < amountRoles.length; i++) // Iterates through amountRoles
{
    System.out.print("[" + i + "]");
    for(int j = 0; j < amountRoles[i]; j++) // Loop through amountRoles[i]
    {
        System.out.print("" + "*");
    }
    System.out.println(" " + amountRoles[i]);
}