[英]java printing pattern to a file using PrintWriter
I am writing a JAVA program to print a Diamond pattern to a file. 我正在编写一个JAVA程序,以将菱形图案打印到文件中。 The pattern is correctly printed on the console but its getting printed properly to the file.
模式已正确打印在控制台上,但已正确打印到文件中。 Here is the code:
这是代码:
static void print_row(int cnt,PrintWriter output)
{
while(cnt --> 0)
System.out.print("* ");
output.print("* "); // or for loop, I just think --> is cute
output.println();
System.out.println();
}
static void diamond(int maxrow, int row,PrintWriter output)
{
if (row >= maxrow)
{
print_row(row,output);
}
else
{
char[] chart = new char[maxrow-row];
Arrays.fill(chart,' ');
String t = new String(chart);
System.out.print(t);
output.print(t);
print_row(row,output);
diamond(maxrow, row+2,output);
char[] chard = new char[maxrow-row];
Arrays.fill(chard,' ');
String d = new String(chard);
output.print(d);
System.out.print(d);
print_row(row,output);
}
}
the output is like this 输出是这样的
*
*
*
*
*
*
*
*
*
The while
loop in print_row
only loops over System.out.println
. print_row
的while
循环仅循环遍历System.out.println
。 To get the same output on the terminal in the the PrintWriter
, you should include it in the loop too: 为了在
PrintWriter
的终端上获得相同的输出,您也应该将其包括在循环中:
static void print_row(int cnt, PrintWriter output)
{
while(cnt --> 0) {
System.out.print("* ");
output.print("* "); // Here!
}
output.println();
System.out.println();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.