如何让我的百分比在我的骰子计划中工作?

时间:2014-01-31 06:21:20

标签: java arrays dice die

run:
How many dice do you want to roll: 3

How many sides on die number 1: 5
How many sides on die number 2: 4
How many sides on die number 3: 6

How many times do you want to roll: 65

Results

[3]      1   0.0% 
[4]      2   0.0% 
[5]      4   0.0% 
[6]      6   0.0% 
[7]      4   0.0% 
[8]      12  0.0% 
[9]      12  0.0% 
[10]     8   0.0% 
[11]     12  0.0% 
[12]     0   0.0% 
[13]     2   0.0% 
[14]     2   0.0% 
BUILD SUCCESSFUL (total time: 7 seconds)

我正在试图弄清楚如何计算第二列到第三列的百分比。

这就是我所拥有的,但我知道我需要做别的事情。

我宁愿偏离使用该hashmap而只是使用更正确的问题。


 for (int i = minRoll; i < maxRoll; i++) {
            int dicer = sumArray[i];
            double percent = dicer/numRol;
            System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);

        }

Ninja编辑:这是我的其余代码

import java.util.Scanner;

/**
 *
 * @author joe
 */
public class DiceSimulator {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);



        System.out.print("How many dice do you want to roll: ");
        int amountOfDie = input.nextInt();
        System.out.println("");
        //declare diceArray

        Die[] diceArray = new Die[amountOfDie];

        //create a new Die for each reference

        int maxRoll = 0;
        for (int i = 0; i < diceArray.length; i++) {

            System.out.print("How many sides on die number " + (i + 1) + ""
                    + ": ");
            int numSides = input.nextInt();
            diceArray[i] = new Die(numSides);
            int minRoll = amountOfDie;
            maxRoll += numSides;

        }
        int minRoll = amountOfDie;

        // int[] sumArray = new int[maxRoll + 1];//to store sum

        System.out.println("");
        System.out.print("How many times do you want to roll: ");
        int numRol = input.nextInt();

        System.out.println("\nResults");

        int[] sumArray = new int[maxRoll + 1];

        for (int i = 0; i < numRol; i++) {
            int sum = 0;
            for (Die d : diceArray) {
                int roll = d.roll();
                sum += roll;

            }
           sumArray[sum]++;           
           }

        System.out.println("");

        for (int i = minRoll; i < maxRoll; i++) {
            int dicer = sumArray[i];
            double percent = (dicer/numRol)*100;
            System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);

        }
    }
}

4 个答案:

答案 0 :(得分:3)

您正在使用整数算术,这意味着您的结果会在保存到int变量之前转换为percent
为了避免这种情况,只需输出其中一个变量:

double percent = (double)dicer / numRol;

正如@PaulHicks所说,你真的应该乘以100。您可以这样做,将其声明为浮点文字(100.0),以避免完全投射:

double percent = 100.0 * dicer / numRol;

答案 1 :(得分:0)

更改此

double percent = dicer/numRol;

double percent = ((double)dicer/numRol)*100;

答案 2 :(得分:0)

dicer / numRol将始终返回0,因为dicer和numRol是整数,而numRol&gt;切丁酶!

要获得百分比结果,您必须将dicer的类型更改为double而不是int

取代:

int dicer = sumArray[i];

使用:

double dicer = sumArray[i];

答案 3 :(得分:0)

或者,您可以通过远离浮点数学来提高准确度:

int percentX10 = (1000 * dicer) / numRol;
String percentString = String.format("%d.%d", percentX10 / 10, percentX10 % 10);