[英]how to make a method print out the amount of times a specific number was rolled?
i have to make a dice program. 我必须做一个骰子程序。 the program asks how many times you want to roll the dice, and then it asks what number you want the statistics for (2-12).
程序会询问您要掷骰子多少次,然后询问您要统计的数字是(2-12)。 it then rolls the dice (x) amount of times and gives you the number rolled each time.
然后掷骰子(x)次数,并为您提供每次掷骰的次数。 then it'll tell you the amount of times each number (2-12) was rolled, and it's supposed to finish with the program telling you what the percentage of the number you asked to see statistics for (example: of the 25 dice rolls, 2 rolls added to a total of 10. the percentage of 10s rolled is 8%).
然后它会告诉您每个数字(2-12)滚动的次数,并且应该以该程序结束,告诉您要查看其统计信息的数字的百分比(例如:25个骰子掷骰,总共添加了10卷,共2卷。10卷的百分比为8%)。
i have to use methods for this program, and my statistics method prints 1 instead of however many times 7 shows up (i'm using 7 as a sort of tester for what i'm doing since 7 is supposed to pop up a lot during dice rolls). 我必须为该程序使用方法,而我的统计方法却显示1而不是多次显示7(我使用7作为我正在做的事情的一种测试器,因为应该在7期间弹出很多骰子卷)。
i'm using drjava and i'm not supposed to be using a debugger for my programs since our instructor hasn't taught us how to do it yet! 我正在使用drjava,并且我不应该为我的程序使用调试器,因为我们的讲师还没有教我们如何做! i think my loop is the problem because i'm trying to get d to print.
我认为我的循环是个问题,因为我正在尝试打印d。
also, how would i find the percentage of times that my selected number was rolled? 另外,我如何找到所选号码被滚动的次数百分比?
import java.util.*;
public class EZD_diceRoll
{
public static void main(String args[])
{
EZD_diceRoll.roll();
EZD_diceRoll.statistics(d);
}
private static int die1;
private static int die2;
private static int total;
public static int number;
public static int d;
public static int times;
public static void roll()
{
Random nums = new Random();
Scanner kbReader = new Scanner(System.in);
System.out.print("How many times would you like to roll?");
times = kbReader.nextInt();
System.out.print("What number (dice roll total) do you want the statistics
for (2 - 12)?");
number = kbReader.nextInt();
for(int a = 1; a < times + 1; a++)
{
die1 = 1 + nums.nextInt(6);
die2 = 1 + nums.nextInt(6);
total = die1 + die2;
System.out.println("Dice roll #" + a + " is " + total);
}
}
public static void statistics(int d)
{
for(int c = 0; c < times + 1; c++)
{
if(total == 7)
{
d++;
}
}
System.out.println("The number of 7's thrown was " + d);
}
}
Use a java.util.HashMap
, 使用
java.util.HashMap
,
HashMap<Integer, Integer> map = new HashMap<>();
The key represents the specific number, the value represents the frequecy. 键代表特定数字,值代表频率。 When you get a number, check if it exists in the map.
当您得到一个数字时,请检查地图中是否存在该数字。 If not, put it into the map with initial frequency
1
. 如果不是,则以初始频率
1
将其放入地图中。 Otherwise, increment the requency. 否则,增加频率。
initialize counters(int var) for dice output values(2-12) and use switch statement inside for loop ,for each roll output give a case inside switch and increment counter for each case and these counter values give number of times the value is rolled. 为骰子输出值(2-12)初始化计数器(int var),并在for循环内使用switch语句,对于每个滚动输出,在开关内提供一个case,并为每种情况提供递增计数器,这些计数器值给出该值被滚动的次数。
the below code prints for each roll if u want only final number just use all system.out.print statements outside(after) for loop instead of using them in case statement. 如果您只希望最后一个数字,则下面的代码将为每卷打印
int[] count=new int[10] //counter for values 2-12
for(int a = 0; a < times + 1; a++)
{
die1 = 1 + num.nextInt(6);
die2 = 1 + num.nextInt(6);
total = die1 + die2;
switch(total)
{
case 2:
count[0]++;
System.out. print("dice 2 rolled" +count[0] +" time");
break;
case 3:
count[1]++;
System.out. print("dice 3 rolled" +count[1] +" time");
break;
case 4:
count[2]++;
System.out. print("dice 4 rolled" +count[2] +" time");
break;
.
.
.
.
case 12:
count[10]++;
System.out. print("dice 12 rolled" +count[10] +" time");
break;
} } }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.