简体   繁体   English

Java:数组星号打印值

[英]Java: Array asterisk printing for values

I am trying to print asterisks based on the number of times a dices side is rolled (all through an array). 我试图根据骰子面滚动的次数(全部通过数组)来打印星号。 I am coming across an issue with the printing of the dice side (i) before the asterisks. 我在星号之前将骰子面(i)的打印遇到问题。 Also, I'm getting two zeroes out of nowhere and don't know where they're coming from. 另外,我从无处获得两个零,也不知道它们从何而来。 I'd appreciate the help. 非常感谢您的帮助。

My code: 我的代码:

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

My output: 我的输出:

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

An example output I am aiming for: 我的目标输出示例:

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

You're starting at zero? 您从零开始?

also for the mid line 也用于中线

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

the above can be removed and just add ln to the previous output. 可以删除上面的内容,只需将ln添加到先前的输出中即可。

I'd also use a variable to keep count of the string and then append it. 我还要使用一个变量来计数字符串,然后附加它。

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

Only answering your question an no other problems I see with the code, you are getting the 0 0 from int i = 0 . 仅回答您的问题,我在代码中看不到其他问题,您正在从int i = 0获得0 0 int i = 0 Change this to int i = 1 and no more 0 0 ! 将其更改为int i = 1且不再为0 0

To get your values to print the way you need, you just need to simple move what you want to print before the * above the loop. 为了使您的值以所需的方式打印,您只需要简单地将要打印的内容移动到循环上方的*之前。 In this case [i] , and move the amount each roll was below the * loop. 在这种情况下[i] ,将每卷的移动量移动到*循环以下。 Code is below. 代码如下。 This should fix your stated problems. 这应该可以解决您陈述的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM