简体   繁体   English

使用随机整数

[英]Working with random integers

Here is the assignment: For this program, there are two "parts". 这是作业:该程序有两个“部分”。 The first part will run the trials and determine how many caps each trial opens before finding a winning cap. 第一部分将运行试验并确定每个试验打开多少个上限,然后再找到获胜的上限。 Every trial (person) will be a winner. 每个审判(人)将成为赢家。 The number of caps opened by each trial is written to the file. 每次试验打开的瓶盖数量将写入文件。 The second part of the program will read in the values and calculate the average. 程序的第二部分将读取这些值并计算平均值。 The average should be between 4.5 and 5.5 since there is a 1 in 5 chance of winning. 平均值应该在4.5到5.5之间,因为有五分之一的获胜机会。

It compiles and runs, but the average is always 0. 它可以编译并运行,但平均值始终为0。

My code: 我的代码:

  int randNum = 0; 
  Random randNumList = new Random(); 

  int counter = 0;
  Scanner in = new Scanner(System.in);
  System.out.println("How many trials will there be?");
  int trials = in.nextInt();

  int winner = 0;

  PrintWriter outFile = new PrintWriter (new File("cap.txt")); 
  //run trials
  for (int loop = 1; loop <= trials; loop++)    
  {  
    //select random number until 5 is selected
    randNum = randNumList.nextInt(6);
    for (randNum = randNumList.nextInt(6); randNum == 5; randNum++)
    { 
      randNum = randNumList.nextInt(6);
      counter++;
    }

    outFile.println(loop + " " + randNum);
  }   


  outFile.close ( );    //close the file when finished 

  String token = " ";
  Scanner inFile = new Scanner(new File("cap.txt")); 

  while (inFile.hasNext())
  {      
       token = inFile.next();
       if(token.equals("5"))
         winner++;  
  }

  double average = winner/counter;
  System.out.println("The average number is " + average);

您的cap.txt文件不包含“ 5” =>获胜者= 0,平均值= 0 /计数器= 0(始终)。

winner/counter returns an int not a double (integer division because both operands are integer so the result is truncated). winner/counter返回一个int而不是double(整数除法,因为两个操作数都是整数,因此结果被截断)。 Try this: 尝试这个:

winner/(double)counter

That does return a double. 那确实返回了两倍。

Apart from the int/int division accuracy problem which should be winner/(double)counter or (double)winner/counter try changing your inner for loop to a do while . 除了int / int除法精度问题(应该是winner/(double)counter(double)winner/counter尝试将内部for loop更改为do while In general, prefer while when you don't know the exact number of iterations. 在一般情况下,宁愿while当你不知道迭代的确切数目。

Also, randNumList.nextInt(6) is [0-5], thus there are 6 possible outcomes -> there is a 1 in 6 chance of winning . 另外,randNumList.nextInt(6)为[0-5],因此有6种可能的结果-> 六分之一的获胜机会 To correct this, use randNumList.nextInt(5) + 1 若要更正此问题,请使用randNumList.nextInt(5)+ 1

for (int loop = 1; loop <= trials; loop++) {  
    //select random number until 5 is selected
    do {
        randNum = randNumList.nextInt(5) + 1;
        counter++;
    } while (randNum != 5);

    outFile.println(loop + " " + randNum); //why here?? maybe you should add it below counter++;
}  

also if(token.equals("5")) won't work, as you write (loop + randNum) , it should work if you use outFile.println(randNum); if(token.equals("5"))也不起作用,那么当您编写(loop + randNum)时 ,如果您使用outFile.println(randNum); ,它应该可以工作outFile.println(randNum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM