我想将数组的每个元素写入一个文本文件。例如下面将演示得更清楚
String[] Name = {"Eric","Matt","Dave"}
Int[] Scores = {[45,56,59,74],[43,67,77,97],[56,78,98,87]}
double[] average = {45.7,77.3,67.4}
我想要在文本文件中包含以下内容
Student Eric scored 45,56,59,74 with average of 45.7
Student Matt scored 43,67,77,97 with average of 77.3
Student Dave scored 56,78,98,87 with average of 67.4
我创建了输出文件
PrintStream output = new PrintStream(new File("output.txt"));
我使用了for循环
for(int i =0;i<=Name.length;i++){
output.println("Student " + Name[i] + " scored " + Scores[i] + " with average of " + average[i]);
}
但这并没有奏效。请帮忙。
最佳答案
我的猜测是编译器不喜欢这一行:
Int[] Scores = {[45,56,59,74],[43,67,77,97],[56,78,98,87]}
Java 中没有 Int
类型。假设您的意思是 int
,编译器仍然会提示,因为 [45,56,59,74]
不是 int!
您需要的是一个 int[][]
和一个声明,例如:{{45,56,59,74}}
不过,我不确定您会对输出感到满意...
关于java - 如何用Java将一维和二维数组写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15035868/