简体   繁体   English

如何获得我的百分比以在Dice程序中工作?

[英]How do I get my percent to work in my Dice program?

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)

I'm trying to figure out how to calculate the percent of the 2nd column to the third. 我试图弄清楚如何计算第二列到第三列的百分比。

Here is what I have, but I know I need to do something else. 这是我所拥有的,但是我知道我需要做些其他的事情。

I would rather stray from using that hashmap and just use a more correct problem. 我宁愿偏离使用该哈希图,而只使用一个更正确的问题。


 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 Edit: Here is the rest of my code 忍者编辑:这是我其余的代码

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

        }
    }
}

You are using integer arithmetics, which means that your result gets converted to an int before being saved in the percent variable. 您正在使用整数算术,这意味着您的结果将被转换为int然后再保存到percent变量中。
To avoid this, just cast one of the variables like this: 为了避免这种情况,只需将以下变量之一转换为:

double percent = (double)dicer / numRol;

As @PaulHicks says, you really should multiply by 100 . 正如@PaulHicks所说,您真的应该乘以100 You can do it like this, by declaring it as a floating point literal ( 100.0 ), to avoid casting altogether: 您可以这样声明,将其声明为浮点文字( 100.0 ),以避免完全强制转换:

double percent = 100.0 * dicer / numRol;

Change this 改变这个

double percent = dicer/numRol;

to

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

dicer/numRol will always return 0 since dicer and numRol are integers, and numRol > dicer! dicer / numRol将始终返回0,因为dicer和numRol是整数,并且numRol> dicer!

To get a percentage result, you have to change the type of dicer to double instead of int 要获得百分比结果,必须将切块机的类型更改为double而不是int

replace: 更换:

int dicer = sumArray[i];

with: 与:

double dicer = sumArray[i];

Alternatively, you could increase accuracy by staying away from floating point maths: 另外,您可以不使用浮点数学来提高准确性:

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

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

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