[英]Is there a way to print all the values of the array to an output file?
我制作了一个RainFall类,用于存储全年降雨量(以英寸为单位)(总共12个值)。 我有它工作,但现在我需要将这些值打印到outputFile(我称之为RainFall.txt )。 当我运行代码并检查文件时,它只存储数组的第一个值。
我已经尝试过try and catch
方法但它没有打印任何东西。
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
public class RainFallMain {
public static void main(String[] args) throws FileNotFoundException {
// Array representing rainfall figures
// position correlates to the month
double[] thisYear = { 1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7 };
RainfallClass r = new RainfallClass(thisYear);
PrintWriter outputFile = new PrintWriter("RainFall.txt");
for (int i = 0; i < thisYear.length; i++)
{
outputFile.println(thisYear[i]);
outputFile.close();
}
输出文件应列出所有12个值。
只有在完成所有写入后才应关闭输出文件。
public static void main(String[] args) throws FileNotFoundException {
// Some statements
for (int i = 0; i < thisYear.length; i++){
outputFile.println(thisYear[i]);
}
outputFile.close(); // Moved out of the for loop
// Some more statements if there.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.