随机数文件输出 /** 这个类模拟掷一对骰子10000次和10000次 统计每种不同类型的翻倍卷的次数 双打。 */ 导入java.util.Random//使用随机数生成器 导入java.util.Scanner; 导入java.io.*; 导入org.omg.PortableServer.ImplicitActivationPolicyOperations; 公共类模拟 { 公共静态void main(字符串[]args)引发IOException { 扫描仪键盘=新扫描仪(System.in); final int NUMBER=10000;//掷骰子的次数 //一种用于模拟掷骰子的随机数发生器 随机生成器=新随机(); int die1Value;//第一个模具上的斑点数 int die2Value;//第二个模具上的斑点数 int count=0;//掷骰子的次数 int snakeEyes=0;//蛇眼滚动的次数 int twos=0;//翻滚double two的次数 int threes=0;//滚动双三次次数 int fours=0;//翻滚double fours的次数 int fives=0;//翻滚双五的次数 int-sixes=0;//滚动双六的次数 字符串文件名;//要写入/读取的文件名 int累加器=0; 双平均值=0; int count2=0; 字符串用户输入; //为任务#3添加代码 System.out.println(“请输入文件的特定路径”); filename=keyboard.nextLine(); PrintWriter outputFile=新的PrintWriter(文件名); //循环,而计数小于次数 //按照任务3中的说明调整while循环中的代码 while(计数
通过将不同的信息写入该文件 谁说必须打印到循环中的输出文件 您已经在该循环中收集了计数器统计信息(以便在循环后打印)。只需对输出文件执行相同的操作随机数文件输出 /** 这个类模拟掷一对骰子10000次和10000次 统计每种不同类型的翻倍卷的次数 双打。 */ 导入java.util.Random//使用随机数生成器 导入java.util.Scanner; 导入java.io.*; 导入org.omg.PortableServer.ImplicitActivationPolicyOperations; 公共类模拟 { 公共静态void main(字符串[]args)引发IOException { 扫描仪键盘=新扫描仪(System.in); final int NUMBER=10000;//掷骰子的次数 //一种用于模拟掷骰子的随机数发生器 随机生成器=新随机(); int die1Value;//第一个模具上的斑点数 int die2Value;//第二个模具上的斑点数 int count=0;//掷骰子的次数 int snakeEyes=0;//蛇眼滚动的次数 int twos=0;//翻滚double two的次数 int threes=0;//滚动双三次次数 int fours=0;//翻滚double fours的次数 int fives=0;//翻滚双五的次数 int-sixes=0;//滚动双六的次数 字符串文件名;//要写入/读取的文件名 int累加器=0; 双平均值=0; int count2=0; 字符串用户输入; //为任务#3添加代码 System.out.println(“请输入文件的特定路径”); filename=keyboard.nextLine(); PrintWriter outputFile=新的PrintWriter(文件名); //循环,而计数小于次数 //按照任务3中的说明调整while循环中的代码 while(计数,java,Java,通过将不同的信息写入该文件 谁说必须打印到循环中的输出文件 您已经在该循环中收集了计数器统计信息(以便在循环后打印)。只需对输出文件执行相同的操作 除此之外:阅读有关数组的内容。当您开始使用变量名(如a1、a2等)时,使用数组通常会使此类代码变得更简单。如果我理解正确,您可能希望在输出文件.close()之前包含此代码。 我还建议您检查您的代码,通过使用方法减少重复代码,并在必要时调用它们。假设您尝试为每个双卷输出代码,只需将打印到文件的代码移出while循环,并使其打印蛇眼、二、三等的最终结果
除此之外:阅读有关数组的内容。当您开始使用变量名(如a1、a2等)时,使用数组通常会使此类代码变得更简单。如果我理解正确,您可能希望在
输出文件.close()之前包含此代码。
我还建议您检查您的代码,通过使用方法减少重复代码,并在必要时调用它们。假设您尝试为每个双卷输出代码,只需将打印到文件的代码移出while循环,并使其打印蛇眼、二、三等的最终结果。您是否尝试输出双打或2个骰子的所有组合的掷骰数?只有双打谢谢。我之前尝试过这个,但没有与其他输出紧密相连。
/**
This class simulates rolling a pair of dice 10,000 times and
counts the number of times doubles of are rolled for each different
pair of doubles.
*/
import java.util.Random; //to use the random number generator
import java.util.Scanner;
import java.io.*;
import org.omg.PortableServer.ImplicitActivationPolicyOperations;
public class DiceSimulation
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
final int NUMBER = 10000; //the number of times to roll the dice
//a random number generator used in simulating rolling a dice
Random generator = new Random();
int die1Value; // number of spots on the first die
int die2Value; // number of spots on the second die
int count = 0; // number of times the dice were rolled
int snakeEyes = 0; // number of times snake eyes is rolled
int twos = 0; // number of times double two is rolled
int threes = 0; // number of times double three is rolled
int fours = 0; // number of times double four is rolled
int fives = 0; // number of times double five is rolled
int sixes = 0; // number of times double six is rolled
String filename; //name of file to write/read
int accumulator = 0;
double mean = 0;
int count2 = 0;
String userInput;
// add code for TASK #3
System.out.println("Please enter a specific path to a file");
filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);
//loop while count is less than NUMBER of times
//adjust code in while loop as instructed in TASK #3
while(count < NUMBER)
{
//roll the dice
die1Value = generator.nextInt(6)+1;
die2Value = generator.nextInt(6)+1;
//Check to see if you have doubles
//Enter code for Task 1 if statements here
if (die1Value == die2Value);
if (die1Value == 1){
snakeEyes++;
outputFile.println ("Snake eyes occurred a total of " + snakeEyes + " times");}
else if(die1Value == 2){
twos++;}
else if(die1Value == 3){
threes++;}
else if(die1Value == 4){
fours++;}
else if(die1Value == 5){
fives++;}
else if(die1Value == 6){
sixes++;}
count++;
} //while(count < NUMBER) ending braces;
//add code that closes the output file for TASK #3 here/////////////////
outputFile.close();
System.out.println ("Output for while loop");
System.out.println ("You rolled snake eyes " + snakeEyes +
" out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes +
" out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours +
" out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives +
" out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes +
" out of " + count + " rolls.");
//Enter code for TASK #2 here for do while loop
do{
System.out.println("Count: " + count);
} while (count < NUMBER);
//roll the dice
die1Value = generator.nextInt(6)+1;
die2Value = generator.nextInt(6)+1;
//Check to see if you have doubles
//Enter code for Task 1 if statements here
System.out.println ("\nOutput for do while loop\n");
System.out.println ("You rolled snake eyes " + snakeEyes +
" out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes +
" out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours +
" out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives +
" out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes +
" out of " + count + " rolls.");
// Enter code for TASK #2 here for for loop
for(count = 1; count <= NUMBER; count++)
break;
System.out.println ("Output for, for loop\n");
System.out.println ("You rolled snake eyes " + snakeEyes +
" out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes +
" out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours +
" out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives +
" out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes +
" out of " + count + " rolls.");
// add code for TASK #4 here
}
}
//add code that closes the output file for TASK #3 here/////////////////
outputFile.println ("Snake eyes occurred a total of " + snakeEyes + " times");
outputFile.println ("Twos occurred a total of " + twos + " times");
outputFile.println ("Threes occurred a total of " + threes + " times");
outputFile.println ("Fours occurred a total of " + fours + " times");
outputFile.println ("Fives occurred a total of " + fives + " times");
outputFile.println ("Sixes occurred a total of " + sixes + " times");
outputFile.close();