将int数组转换为double来计算百分比

时间:2012-03-19 20:06:17

标签: java arrays casting

我正在编写一个使用数组,math.random和随机生成器的初学者程序来计算2次掷骰一定次数的总和。

我试图计算总和出现的次数百分比,需要2位小数。有没有办法转换(?)和数组,使其显示2位小数?

另外,当我在屏幕上打印时,每个总和显示120次。我不明白为什么

这是我到目前为止所做的:

import java.util.Random;
import java.util.Scanner;
public class Die {

    public static void main(String[] args){

        int faces;
        int face1;
        int face2;
        int face3;
        int face4;
        int times;
        int index;
        int sum;



        Scanner scan = new Scanner(System.in);
        Random rand = new Random();

        System.out.println("%-0-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0-%");
        System.out.println("%\t\t\t\t\t    %");
        System.out.println("%  How good is the Random Number Generator  %");
        System.out.println("%\t\t\t\t\t    %");
        System.out.println("%-0-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0-%");

        System.out.println();
        System.out.print("What is the number of sides of each die? ");
        faces = scan.nextInt();
        System.out.print("How many times do you want to roll the dice ?");
        times = scan.nextInt();


        int[]rollCount = new int[(faces*2) + 1];

        for (int i = 0; i<rollCount.length; i++)
            rollCount[i] = 0;

                int dice1=faces;
                    for (int k=1; k<=dice1; k++){
                        int dice2=faces;
                            for (int r=1; r<=dice2; r++){

                    rollCount[k+r]++;

                    for (int roll = 0; roll <times; roll++){
        }

    }
}




        int[]rollCountRand = new int[(faces*2) + 1];
        for (index = 0; index <rollCountRand.length; index++)
            rollCountRand[index] = 0;


        for (int roll = 0; roll<=times; roll++){
            face1 = 1 + rand.nextInt(faces);
            face2 = 1 + rand.nextInt(faces);
            rollCountRand[face1 + face2]++;
        }


        int[]rollCountMath = new int [(faces*2)+1];

        for (int i=0; i<rollCountMath.length; i++)
            rollCountMath[i] = 0;

        for (int j=0; j<=times; j++){

            face3 = (int)(faces* Math.random() + 1);
            face4 = (int)(faces* Math.random() + 1);
            sum = face3 + face4;
            rollCountMath[sum]++;
        }



        for (int r = 2; r <rollCount.length; r++){

             int percent = (rollCount[r] / ((faces*faces)*100));

        for (int k = 2; k <rollCountRand.length; k++){

             int percentOne = rollCountRand[k] / (times/100);

            for (int q = 2; q <rollCountMath.length; q++){

                int percentTwo =rollCountMath[q] / (times/100);





        System.out.print(r + "\t( " + rollCount[r] + ")"  + "\t" + "%" + percent + "\t" + k + "\t( " + rollCountRand[k]+")" + "\t"+"%"+percentOne + "\t" + q + "\t( " + rollCountMath[q]+")" + "\t" + "%"+percentTwo);
            System.out.println();

谢谢!

1 个答案:

答案 0 :(得分:2)

您无法直接将int[]投射到double[],但您可以在处理每个元素时对其进行投射,例如,在计算百分比的情况下,您可以编写int percentTwo = (int)Math.round((double)rollCountMath[q] / (times/100.0));。< / p>

至于循环计数,看起来你在这里张贴的代码缺少一些结束括号(当你粘贴代码时可能只是格式化错误),所以很难说如果括号到位它会如何表现。