Java:基于舍入的循环和打印

标签 java arrays loops if-statement random

我无法弄清楚如何根据相应的(输出右侧的数字)计数来显示四舍五入的星号。

我试图用 1 个星号代表 100 个星号。但是,当我得到一个数字,比如 roll #17 是 417,我希望它只打印 4 个星号,而不是 5 个;同样的四舍五入。我尝试使用 Math.round() 但没有成功。

如果有任何帮助,我将不胜感激。

我的代码:

public class Histogram {

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

        int numRoles = 30000;
        int[] amountRoles = new int[19]; // amountRoles Holds the Array

        for (int i = 3; i < 7; i++) 
        amountRoles[i] = 0; // Set 0
        {
            for (int i = 0; i < numRoles; i++) 
            {
                int die1 = (int)(Math.random()*6+1);
                int die2 = (int)(Math.random()*6+1);
                int die3 = (int)(Math.random()*6+1);
                amountRoles[die1+die2+die3]++; // Increments
            }
            System.out.print("The die was rolled " + numRoles + " times, its six value's counts are:");
            for (int i = 3; i < 7; i++)
            {
                System.out.println(); // Line Holder
            }
        }
        for (int i = 3; i < amountRoles.length; i++) // Iterates through amountRoles
        {
            System.out.print("[" + i + "]" + "  ");
            for(int j = 0; j < amountRoles[i]; j++) // Loop through amountRoles[i]
            {
                if (Math.round(j) % 100 == 0)
                {
                    System.out.print("" + "*");
                }
            }
            System.out.println(" "  + amountRoles[i]);
        }
    }
}

我的输出:

[3]     ** 139
[4]     **** 389
[5]     ********* 826
[6]     ************** 1366
[7]     ********************* 2082
[8]     ****************************** 2973
[9]     *********************************** 3440
[10]    *************************************** 3859
[11]    ************************************** 3742
[12]    *********************************** 3482
[13]    ****************************** 2918
[14]    ******************** 1996
[15]    ************** 1341
[16]    ********* 865
[17]    ***** 417
[18]    ** 165

最佳答案

这是打印每一行的部分:

System.out.print("[" + i + "]" + "  ");
for(int j = 0; j < amountRoles[i]; j++) // Loop through amountRoles[i]
{
    if (Math.round(j) % 100 == 0)
    {
        System.out.print("" + "*");
    }
}
System.out.println(" "  + amountRoles[i]);

您正在循环数百次。这是不必要和低效的。只需使用除法即可获得要打印的 *s 的数量:

System.out.print("[" + i + "]  ");
int starsToPrint = (int) Math.round((double) amountRoles[i] / 100.0);
for (int j = 0; j < starsToPrint; j++) {
    System.out.print("*");
}
System.out.println(" "  + amountRoles[i]);

仅供引用,原始代码被破坏的原因是因为 0 % 100 == 0,因此在内部 for 循环的第一次迭代中它会打印一个额外的“*”。

关于Java:基于舍入的循环和打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539421/

相关文章:

java - 如何在 Github 包中添加我的 Android 库的依赖项?

java - 在bat文件中捕获java异常

r - 在家庭网格中检索夫妇 ID

java - 安卓异常说明

javascript排序,比较3种不同类型并按顺序排序

c - 所有 char 数组都自动以 null 结尾吗?

C# 如何使用 foreach 循环首先从 array[1] 而不是 array[0] 读取?

java - 使用迭代器的 for 循环和 while 循环之间的区别

python - 尝试在此单行循环中增加 Y

Java:将对象列表转换为其他内容