【问题标题】:Trying to create a Dice Roller Stats app in Java尝试用 Java 创建一个 Dice Roller Stats 应用程序
【发布时间】:2017-12-11 11:19:39
【问题描述】:

我编写了一个应用程序,它有一个子类,它会根据用户输入的面数来掷骰子,并使用一个整数来掷骰子一定次数。

如: 用户输入6面,想掷骰子1000次。

我也应该使用与我编码的数组类似的数组。

我目前拥有的:

public class DDiceRoller {

public static void diceStats() {
    int maxNum = DiceRolling.diceSides;
    Scanner sc = new Scanner(System.in);
    int randomValue = 1 + (int) (Math.random() * maxNum);
    int randomValue2 = 1 + (int) (Math.random() * maxNum);
    int die1 = (randomValue);
    int die2 = (randomValue2);
    int sum = die1 + die2;
    int rollnum;
    int idx;
    System.out.println("Welcome to the Dice Roll Stats Calculator!");
    int[] combinations = new int[maxNum]; //I haven't even used this variable yet, don't know how.

    System.out.println("Enter amount of rolls: ");
    rollnum = sc.nextInt();

    for (idx = 0; idx < rollnum; idx++) {
    System.out.println(sum); //I know this is wrong, just don't know what to do.
    }

  }
}

然后计算器将运行程序并根据程序运行的次数输出百分比哪些结果......所以类似于......

所需的输出:

Total    Count    Percentage
-----  ---------  ----------
   2        123     3.01%    
   3        456     6.07%    
   4        etc     3.19%    
   5        ???     4.45%    
   6        ???     8.90%    
   7        ???     8.62%    
   8        ???     7.63%    
   9        ???     6.92%    
  10        ???     5.40%    
  11        ???     6.96%    
  12        ???     8.36%    

当前输出: 现在我得到的只是输入的相同值,因为我现在对我的“for”循环所做的只是重复“sum”,无论多少次骰子滚动。总和也不会为每次迭代返回不同的数字。

我的主要目标是按用户要求的任意次数掷骰子。使用 Java 数组来存储结果。例如,每次我要执行下一个掷骰时,创建一对新骰子。这样,我可以将每对骰子存储在一个数组中。

我正在尝试学习如何分段编写代码,但我现在迷失了方向,感觉被打败了。不能感谢您您可以提供任何指导。 如果这令人困惑,我真的很抱歉,我不完全理解说明......

【问题讨论】:

  • 你能展示你想要做什么吗?

标签: java arrays


【解决方案1】:

您可以使用 2D 数组或哈希映射来执行此操作,但我更喜欢 2x ArraList

NumberFormat formatter = new DecimalFormat("#0.00");  

ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<Integer> counts = new ArrayList<Integer>();

int maxNum = DiceRolling.diceSides;
Scanner sc = new Scanner(System.in);
int rollnum;
int randomValue;

System.out.println("Welcome to the Dice Roll Stats Calculator!");
System.out.println("Enter amount of rolls: ");
rollnum = sc.nextInt();


for (int i  = 0; i < rollnum; i++) {

    randomValue = (1 + (int) (Math.random() * maxNum)) + (1 + (int) (Math.random() * maxNum));

    if(numbers.contains(randomValue)){
        int position = numbers.indexOf(randomValue);
        counts.set(position, counts.get(position)+1);
    }else{
        numbers.add(randomValue);
        counts.add(1);
    }

}

System.out.println("Total\tCount\tPercentage");
System.out.println("-----\t---------\t----------");
for(int i = 0; i<numbers.size(); i++){
    System.out.println(numbers.get(i) +"\t" + counts.get(i) + "\t" + formatter.format(((double)(counts.get(i)*100))/rollnum) + "%";
}

这是你的答案吗? 我希望这行得通。

【讨论】:

  • 似乎@Prodian 完全按照您的要求完成了。如果您需要一个单独的循环来进行计算,您可以将循环分成两个并根据需要进行操作。
  • 由于某种原因,顶部的“NumberFormat”与 DecimalFormat 和 ArrayList 一起显示错误。 @clinomaniac
  • @clinomaniac 错误:找不到符号 NumberFormat formatter = new DecimalFormat("#0.00"); symbol: class NumberFormat location: class DiceRoller //ArrayList也一样
  • 您需要导入正确的库。在你的类名之前和你的包声明之后添加这些。导入 java.text.DecimalFormat;导入 java.text.NumberFormat;导入 java.util.ArrayList;导入 java.util.Scanner;
  • 一开始我对阅读的内容感到困惑。 Prodian 说我可以用 2D Array 完成这项工作?你熟悉吗?它会帮助我学到更多东西,因为我觉得 ArrayList 有点领先于我。@clinomaniac
猜你喜欢
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 2018-12-18
  • 2014-02-26
  • 2017-11-15
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多