如何使用控制台输入并将其转换为.txt文件?

时间:2014-06-14 20:57:33

标签: java file

我被建议使用此代码将java中生成的信息写入.txt文件。没有错误,但没有创建文件。我正在使用Eclipse和Mac OS X Mavericks。 代码:

import java.io.*;

import java.util.*;

public class Practice13 
{

    public static void main(String[] args) 
    {
        double totalSum = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("How many numbers would you like in the text files?");
        double a = in.nextDouble();
        double b = a;
        System.out.println("How mnay files would you like us to to create?");
        int g1 = in.nextInt();
        g1++;
        System.out.println("Would you like the average of all the numbers and the sum of all the numbers added to the end of the file?");
        System.out.println("(0) No");
        System.out.println("(1) Yes");
        double q = in.nextDouble();
        Random random = new Random();
        while (g1 > 1);
        {
            try
            {
                g1--;
                String g = Integer.toString(g1);
                FileWriter outFile = new FileWriter("/Users/Lam/Documents/"+g+".txt");
                PrintWriter out = new PrintWriter(outFile);
                double counter = 0;
                double totalCounter = 0;
                while (a > 0)
                    {
                    double j = random.nextInt(200000);
                    totalSum = totalSum + j;
                    out.print(j);
                    out.print(", ");
                    a--;
                    counter++;
                    if (counter == 500000)
                        {
                        totalCounter = totalCounter + counter;
                        System.out.println("We are still working and have generated " + totalCounter + "numbers.");
                        counter = 0;
                        }
                    }
                a=b;
                if (q<0)
                {
                    System.out.println("Wow, how did you screw that one up?");
                    System.out.println("We have saved your file without the average and sum.");
                }
                if (q>1)
                {
                    System.out.println("Wow, how did you screw that one up?");
                    System.out.println("We have saved your file without the average and sum.");
                }
                if (q==0)
                {
                    System.out.println("We have not saved your file without the average and sum.");
                }
                if (q==1)
                {
                    System.out.println("We have saved the average and sum to the file.");
                    System.out.println();
                    System.out.println("---------------------------------------------------------------------------------------");
                    System.out.println("The sum of all the random numbers is: " + totalSum);
                    System.out.println("The average of all the random number is: " + totalSum/b);
                }
                System.out.println("The task has completed");

                totalSum = 0;
                out.close();
            }   catch (IOException e)
            {
                 e.printStackTrace();
            }
        }

    }

}

0 个答案:

没有答案