jeudi 14 décembre 2017

How to generate 20 random numbers between 1 & 100, write to text file; read from the file, calculate the average and display to the screen [on hold]

This was some help I was able to find, but it doens't look like it's reading from the file...Any help?

import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class Lab9_1 {

    public static void main(String[] args) throws IOException 
    {
    Scanner input = new Scanner(System.in);
    Random RandomNumber = new Random();
    int num = 0;
    int count = 0;
    int sum = 0;
    int average = 0;
    int counttwo=0;
    String fileName = "random";

    PrintWriter outputFile= new PrintWriter(fileName);

    for (count =0; count<20;count++) 
    {
        num = RandomNumber.nextInt(99)+1;
        sum = sum + num;
        outputFile.println(num);
    }
    average = sum/20;
    outputFile.close();
    System.out.println("Data written to the file.");

    System.out.print("Average of all numbers: " + average);

    File file = new File(fileName);
    Scanner inputFile = new Scanner (file);

    System.out.println("\nThe random numbers generated are: ");
    for (counttwo=0; counttwo <20; counttwo++)
    {    
        String line = inputFile.nextLine();
        System.out.println(line);
    }
    inputFile.close();
    }

}




Aucun commentaire:

Enregistrer un commentaire