For循环可永久打印到文件,即使关闭也将文件空白?

时间:2018-10-07 04:48:31

标签: java for-loop printwriter

我正在尝试编写代码,通过从10个中选择一个随机数来模拟1000次以上的蒙特卡洛方法,直到该数字为10,每次的计数器(观察到的松鼠数)都会增加直到随机数为10;然后该计数器将被打印到文件中。但是,这是通过在其中包含嵌套循环的For循环完成的,但是该程序似乎永远在此For循环中加载一次,即使使用close(),文件仍为空白。

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
import java.util.Random;
public class AnimalPopulation
{
    public static void main(String[] args) throws IOException
    {
    //declare and initialize variables
    Random randsquirrel = new Random();
    Scanner in = new Scanner(System.in);
    PrintWriter outFile = new PrintWriter(new File ("squirreldata.txt"));

    // User input
    System.out.println("Welcome to the Fox Squirrel Simulator");
    System.out.println("\n");
    System.out.println("How many trials should be simulated?");
    System.out.println("Enter a value greater than 1000:");
    int trialcounter = in.nextInt();

    // Input does not match requirements
    while (trialcounter <= 1000)
    {
        System.out.print("\n\n  Please try again. Enter a number greater than 1000.");
        System.out.println();
        System.out.println("How many trials should be simulated?");
        System.out.println("Enter a value greater than 1000:");
        trialcounter = in.nextInt();
    }
    System.out.println("\nsimulating trials now... one moment please ...");

    // Experiment with ratio of 1/10 fox squirrels 
    int randomsquirrel = 0;
    int totalsquirrels = 0;

    for (int i = 1; i <= trialcounter; i++)
    {
        randomsquirrel = randsquirrel.nextInt(10)+1;
            while (randomsquirrel != 10)
            {

                totalsquirrels++;
            }
                if (randomsquirrel == 10);
                {
                    totalsquirrels++;
                }
        outFile.println(totalsquirrels);
    }
    outFile.close();

    // Read file and print result
    File readfile = new File ("squirreldata.txt");
    Scanner readFile2 = new Scanner(readfile);
    Double trialtotalsquirrels = 0.0;
    while(readFile2.hasNextLine())
    {
        String token = readFile2.nextLine();
        int totalsquirrels2 = Integer.parseInt(token);
        trialtotalsquirrels += totalsquirrels2;
    }
    readFile2.close();

    System.out.println();
    System.out.println("The results!");
    System.out.println("The average number of squirrels observed until\n spotting a Fox Squirrel at the city part is: " + trialtotalsquirrels/trialcounter);
    }
}

2 个答案:

答案 0 :(得分:1)

你能告诉我这是怎么回事吗?

    randomsquirrel = randsquirrel.nextInt()+1;
    while (randomsquirrel != 10)
    {
        totalsquirrels++;
    }

虽然循环由3部分组成,而while关键字,(条件)和{要运行的行}

while (condition) {
    //lines to run.
}

首先检查条件,如果条件已转,它将执行所有行。 完成所有行后,它将再次检查条件。如果条件仍然成立,它将再次运行所有行。它将持续永远运行这些行,直到条件变为假或中断。

答案 1 :(得分:0)

您使用了不正确的while,因为您永远不会在while循环中更改值,这会导致无限循环。正确的方法:

public static void main(String[] args) throws IOException
{
    SecureRandom randsquirrel = new SecureRandom(); // it is better
    // rest of your code
    int randomsquirrel = 0;
    int totalsquirrels = 0;
    for (int i = 1; i <= trialcounter; i++)
    {
        randomsquirrel = randsquirrel.nextInt(10)+1;
        while (randomsquirrel != 10)
        {                               
            randomsquirrel = randsquirrel.nextInt(10) + 1:
            totalsquirrels++;
            if (randomsquirrel == 10);
                break;
        }                      
    }
    // rest of your code
}