我在课堂上有一个问题,我根本不知道。
这是一个问题:
该测验的目的是加强对使用循环和计数的理解,并审查随机数的使用。
修改以下程序以打印直方图,其中骰子掷骰子的总数等于每个可能的值,通过打印###次数的字符来显示。每个卷将使用两个滴。
示例:
直方图显示每个可能值的骰子卷总数。
DICE滚动统计(结果有所不同):
2s:######
3S:####
4S:###
5s:########
6s:#####################
7s:#############
8s:#############
9s:##############
10s:###########
11s:#####
12s:####
~~~~~~~~~~~~~~~~~~~~
我无法在上面的示例中获得该程序来打印直方图。
这就是我到目前为止所拥有的:
import java.util.Scanner;
import java.util.Random;
public class DiceStats {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
int seedVal = 11;
randGen.setSeed(seedVal);
// FIXME 1 and 2: Set the seed to the Random number generator
int i = 0; // Loop counter iterates numRolls times
int numRolls = 0; // User defined number of rolls
// FIXME 3: Declare and initiate cariables needed
int numOnes = 0;
int numTwos = 0;
int numThrees = 0;
int numFours = 0;
int numFives = 0;
int numSixes = 0; // Tracks number of 6s found
int numSevens = 0; // Tracks number of 7s found
int numEights = 0;
int numNines = 0;
int numTens = 0;
int numElevens = 0;
int numTwelves = 0;
int die1 = 0; // Dice 1 values
int die2 = 0; // Dice 2 values
int rollTotal = 0; // Sum of dice values
System.out.println("Enter number of rolls: ");
numRolls = scnr.nextInt();
if (numRolls >= 1) {
// Roll dice numRoll times
for (i = 0; i < numRolls; ++i) {
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
rollTotal = die1 + die2;
// FIXME 4: Count number of sixs and sevens; complete the same for all other possible values
if (rollTotal == 1) {
numOnes = numOnes + 1;
}
if (rollTotal == 2) {
numTwos = numTwos + 1;
}
if (rollTotal == 3) {
numThrees = numThrees + 1;
}
if (rollTotal == 4) {
numFours = numFours + 1;
}
if (rollTotal == 5) {
numFives = numFives + 1;
}
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
if (rollTotal == 7) {
numSevens = numSevens + 1;
}
if (rollTotal == 8) {
numEights = numEights + 1;
}
if (rollTotal == 9) {
numNines = numNines + 1;
}
if (rollTotal == 10) {
numTens = numTens + 1;
}
if (rollTotal == 11) {
numElevens = numElevens + 1;
}
else if (rollTotal == 12) {
numTwelves = numTwelves + 1;
}
System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 +
"+" + die2 + ")");
}
// Print statistics on dice rolls
System.out.println("nDice roll statistics:");
// FIXME 5: Complete printing the histogram
System.out.println("1s: " + numOnes);
System.out.println("2s: " + numTwos);
System.out.println("3s: " + numThrees);
System.out.println("4s: " + numFours);
System.out.println("5s: " + numFives);
System.out.println("6s: " + numSixes);
System.out.println("7s: " + numSevens);
System.out.println("8s: " + numEights);
System.out.println("9s: " + numNines);
System.out.println("10s: " + numTens);
System.out.println("11s: " + numElevens);
System.out.println("12s: " + numTwelves);
}
else {
System.out.println("Invalid rolls. Try again.");
}
return;
}
}
任何帮助将不胜感激。
具有这样的循环,您有打印语句。
修改您的代码,以便不要每次将它们放在数组中进行新变量,以便您可以通过它们循环。
import java.util.Scanner;
import java.util.Random;
public class DiceStats {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
int seedVal = 11;
randGen.setSeed(seedVal);
// FIXME 1 and 2: Set the seed to the Random number generator
int i = 0; // Loop counter iterates numRolls times
int numRolls = 0; // User defined number of rolls
// FIXME 3: Declare and initiate cariables needed
int[] numValues=new int[12];
int die1 = 0; // Dice 1 values
int die2 = 0; // Dice 2 values
int rollTotal = 0; // Sum of dice values
System.out.println("Enter number of rolls: ");
numRolls = scnr.nextInt();
if (numRolls >= 1) {
// Roll dice numRoll times
for (i = 0; i < numRolls; ++i) {
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
rollTotal = die1 + die2;
// FIXME 4: Count number of sixs and sevens; complete the same for all other possible values
numValues[rollTotal]++;
System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 +
"+" + die2 + ")");
}
// Print statistics on dice rolls
System.out.println("nDice roll statistics:");
// FIXME 5: Complete printing the histogram
for(int i=2;i<=12;i++)
{
System.out.print(i+"s: ");
for(int j=0;j<numVales[i];j++)
{
System.out.print("#");
}
System.out.println();
}
else {
System.out.println("Invalid rolls. Try again.");
}
return;
}
}
让我知道您是否需要有关问题的澄清。
您可以做这样的事情:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//You can directly set the seed during the object creation.
Random random = new Random(System.currentTimeMillis());
// This array is used to keep the value of your dice (2 - 12)
int [] histogram = new int[13];
while(true) {
System.out.println("Enter number of rolls: ");
int numberOfRolls = scanner.nextInt();
//If you enter 0, you can simply terminate the program
if(numberOfRolls == 0) break;
for(int i = 0; i < numberOfRolls; i++) {
int rolledValue = (random.nextInt(6) + 1) + (random.nextInt(6) + 1);
histogram[rolledValue]++;
}
//Print the result to your console.
for(int i = 2; i < histogram.length; i++) {
System.out.print("Total: " + i + " ");
for(int j = 0; j <histogram[i]; j++) {
System.out.print("#");
}
System.out.println();
}
}
}
该代码的结果如下:
输入卷:7
总计:2
总计:3#
总计:4
总计:5 ##
总计:6
总计:7 ###
总计:8
总计:9
总数:10#
总计:11
总计:12
看起来您真的很近。您只需要打印每个INT变量的##。以下将对Numtwos做到这一点:
char[] chars = new char[numTwos];
Arrays.fill(chars, '#');
String result = new String(chars);
System.out.println(result);
您可以将整个东西放在12个循环中以将其打印为所有这些。