【发布时间】:2018-05-05 08:42:29
【问题描述】:
public class dataarrange {
public static void main(String args[]) {
try {
PrintStream myconsole = new PrintStream(new File("D://out.txt"));
for (int i = 0; i < 10; i++) {
double a = Math.sqrt(i);
int b = 10 + 5;
double c = Math.cos(i);
myconsole.print(a);
myconsole.print(b);
myconsole.print(c);
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
}
在这个编程代码中,我生成了一个名为out 的文本文件,我在其中写下了dataarrange class. 的输出代码没有错误。根据代码,我们得到 a,b,c 10 次。我在文本文件中系统地记下该值。文本文件应该看起来像一个有 10 行和 3 列的矩阵。但是当我打开文本文件 out.txt 时,所有数据都是分散的。它们被写成一行而不是矩阵格式。
期望的输出:
a b c
val1 val2 val3
val4 val5 val6
val7 val8 val9
等等……
但是得到输出val1 val2 val3 val4 val5 val6。我该如何解决这个问题?
【问题讨论】:
-
String.format- 可能类似于 this for example, example -
documentation -
println()用于跳到下一行,更好的是printf(...)用于格式化和打印(这是String.format和print) -
是否需要对齐所有列?或者只是打印它们之间有空格?
-
请遵守命名约定。类名应始终以大写字符开头,然后是驼峰式。所以改为
DataRange。
标签: java file tabular printstream