我做了一个乘法表。问题在于该表未按应有的顺序排序。
如果只想在屏幕上打印,则使用此System.out.printf(“%4d”)
。如何使用BufferedWriter
解决此问题?
代替此:
Irj be egy szamot:
5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25 `
我想要这个:
Irj be egy szamot:
5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25 `
这是我的代码:
public class EgyszerEgy {
public static void main(String[] args) {
int a;
int b;
try {
FileWriter writer = new FileWriter("EgyszerEgy.txt");
BufferedWriter bf = new BufferedWriter(writer);
Scanner tastatur = new Scanner(System.in);
System.out.println("Irj be egy szamot: ");
int szam = tastatur.nextInt();
for (a = 1; a <= szam; ++a) {
for (b = 1; b <= szam; ++b) {
int eredmeny = a * b;
String eredmenyString = String.valueOf(eredmeny);
bf.write(eredmenyString);
bf.write(" ");
}
bf.newLine();
}
bf.flush();
bf.close();
} catch (Exception e) {
}
// Kiolvasas
//String result;
try {
FileReader fr = new FileReader("EgyszerEgy.txt");
BufferedReader br = new BufferedReader(fr);
String result;
while ((result = br.readLine()) != null) {
System.out.println(result);
}
br.close();
} catch (Exception e) {
}
}
}