编译通过,为什么没有任何输出?

yifaner22 2009-07-12 11:07:58
这是书上一个现成的例子,编译通过了,但是没有任何输出,敬请各位帮看看到底是哪儿出了问题?谢谢了!

代码如下:类FormattedWriter的代码
import java.io.*;

public class FormattedWriter extends PrintWriter{

public final static int LEFT_JUSTIFIED=1;
public final static int RIGHT_JUSTIFIED=2;
private int justification=RIGHT_JUSTIFIED;
private int width=0;

public FormattedWriter(Writer output,boolean autoflush,
int width,int justification){
super(output,autoflush);
if(width>0)
this.width=width;
if(justification==LEFT_JUSTIFIED||justification==RIGHT_JUSTIFIED)
this.justification=justification;
}

public FormattedWriter(Writer output,int width){
this(output,false,width,RIGHT_JUSTIFIED);
}

public FormattedWriter(Writer output,int width,int justification){
this(output,false,width,justification);
}

public FormattedWriter(Writer output,boolean autoflush,int width){
this(output,autoflush,width,RIGHT_JUSTIFIED);
}

private String pad(String str){
if(width==0){
return str;
}

int blanks =width-str.length();
StringBuffer result=new StringBuffer();

if(blanks<0){
for(int i=0;i<width;i++)
result.append('X');
return result.toString();
}

if(blanks>0)
for(int i=0;i<blanks;i++)
result.append(' ');
result.insert(justification==LEFT_JUSTIFIED ? 0:result.length(),str);
return result.toString();
}

public void print(long value){

super.print(pad(String.valueOf(value)));
}

public void print(double value){
super.print(pad(String.valueOf(value)));
}

public void print(String str){
super.print(pad(str));
}

public void println(int value){
super.println(pad(String.valueOf(value)));
}
/*
public void println(double value){
super.println(pad(String.valueOf(value)));
}

public void println(String str){
super.println(pad(str));
}
*/
public void setWidth(int width){
if(width>=0)
this.width=width;
}

}

类TestFormattedWriter代码如下:

import java.io.*;

public class TestFormattedWriter{

public static void main(String[] args){

int[] numbers={

1,1,2,3,5,8,13,21,34,55,89,144,233,377
};

double[] values={

1.0,1.0,1.414,1.732,2.236,2.828,3.606,4.582,5.831,
-123456789.23456
};

String[] strings={

"one","one","two","three","five","eight","thirteen"
};

FormattedWriter out=new FormattedWriter(
new BufferedWriter(
new OutputStreamWriter(System.out)),
12,FormattedWriter.RIGHT_JUSTIFIED);

for(int i=0;i<numbers.length;i++){

if(i%6==0){
out.println();
}

out.print(numbers[i]);
}

out.setWidth(10);
for(int i=0;i<values.length;i++){
if(i%5==0){
out.println();
}
out.print(values[i]);
}

for(int i=0;i<strings.length;i++){
if(i%4==0){
out.println();

}
//out.print(strings[i],14);
}
}
}
敬请指教!谢谢!
...全文
107 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱摸鱼de老邪 2009-07-12
  • 打赏
  • 举报
回复
有一个方法叫flush,你既然封装了BufferedWriter,不知道输出的时候要调用这个方法么?
linuxlsx 2009-07-12
  • 打赏
  • 举报
回复
顶 1L 可能缓冲区还没满 你把数据量弄大点

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧