在线揭帖:使用类封装操作后就执行出问题了
///////////////////////////////////sort.java
import java.io.*;
public class sort
{
public static void main(String args[]) throws
IOException {
int myArray[]={99,88,77,66,55,44,33,22,11};
sortTest st = new sortTest ( myArray );
st.init(); //初始化写文件的那些对象
// logic operation
st.out_file ( "Before Sorting: " ); // 没有写入文件
st.out_crt ( "before "); // 执行正确
st.sortnub (); // 进行排序
st.out_crt ( "after "); // 执行正确
st.out_file ( "After Sorting: " ); // 没有写入文件
}
}
///////////////////////////////sortTest.java
import java.io.*;
class sortTest
{
// data member
private int myArray[];
private FileWriter fw;
private BufferedWriter bf;
private PrintWriter out;
// member function
sortTest ( int arr[] ) throws IOException
{
myArray = arr;
}
protected void finalize() throws IOException
{
out.close();
}
void init() throws IOException
{
fw = new FileWriter( "hh ");
bf = new BufferedWriter(fw);
out = new PrintWriter(bf);
}
void sortnub()
{
for( int i=0;i <myArray.length;i++){
for( int j=i+1;j <myArray.length;j++){
if ( myArray[i]> myArray[j]){
int temp=myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}
}
}
};
void out_crt ( String s )
{
System.out.println(s);
for (int i=0;i <myArray.length;i++)
System.out.print(myArray[i]+ " ");
System.out.println();
};
void out_file ( String text ) throws IOException
{
out.print(text+ "\n ");
for (int i=0;i <myArray.length;i++)
out.print(myArray[i]);
out.print( "\n ");
};