PrintWriter add text to file

后端 未结 4 1121
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 13:27

In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to

4条回答
  •  长发绾君心
    2021-01-14 13:53

    Two problems:

    1) You are overwriting the file each time. printResultsToFile should take the whole array, not just one datum. Call it from outside your loop.

    2) Consider entering your floating point numbers as 2.08e24 for brevity.

    private void printResultsToFile(double result[]) {
      PrintWriter pw = new PrintWriter(new FileWriter("planetaryData.txt"));
      for (int i=0; i< result.length; i++) {
        pw.printf("%.2f%n", result[i]);
      }
      pw.close();
    }
    

提交回复
热议问题