【问题标题】:How do I get my percent to work in my Dice program?我如何获得在我的 Dice 程序中工作的百分比?
【发布时间】:2014-02-23 18:31:48
【问题描述】:
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)

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

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

我宁愿不使用该哈希图,而只使用更正确的问题。


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

        }

忍者编辑:这是我的其余代码

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

        }
    }
}

【问题讨论】:

    标签: java arrays dice die


    【解决方案1】:

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

    double percent = (double)dicer / numRol;
    

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

    double percent = 100.0 * dicer / numRol;
    

    【讨论】:

    • 这只是给出比率。要获得百分比值,您需要将结果乘以 100。
    • @Keppil 我的第三列仍然显示零。我已经用我的完整代码编辑了我的帖子。是不是我做错了什么?
    • @user3023253:试试我的第二个建议。
    【解决方案2】:

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

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

    【讨论】:

    • 但这有点琐碎:)
    【解决方案3】:

    dicer/numRol 将始终返回 0,因为 dicer 和 numRol 是整数,而 numRol > dicer!

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

    替换:

    int dicer = sumArray[i];
    

    与:

    double dicer = sumArray[i];
    

    【讨论】:

    • 非常感谢你
    【解决方案4】:

    改变这个

    double percent = dicer/numRol;
    

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

    【讨论】:

      猜你喜欢
      • 2018-03-18
      • 1970-01-01
      • 2019-11-10
      • 2022-08-10
      • 2021-01-09
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多