我的程序错在哪里了

来源:百度知道 编辑:UC知道 时间:2024/06/04 19:12:56
以读写方式创建一个RandomAccessFile对象,写入10个随机数,然后读取文件中的第1个和最后一个的随机数并显示出来。
import java.io.*;
public class Exam9_27 {

public static void main(String[] args)throws IOException {

RandomAccessFile rf = new RandomAccessFile("d:\\test.dat", "rw");
int a[] = new int[10];
for (int i = 0; i <10; i++) {
a[i] = (int) (Math.random() * 100);
rf.close();
rf = new RandomAccessFile("d:\\test.dat", "rw");
rf.seek(1*4);
rf.readInt();

System.out.println( rf.readInt());
rf.seek(9*4);
rf.readInt();
System.out.println("Value " + i + ": " + rf.readint());
rf.close();

}

}

}
1楼的好象不合题意啊
加了,我知道,加了rf.writeInt(a[i]);也不行
大哥,你告诉我代码应该怎么写

1.没将随机数写入。
2.你可以跟一遍你写的循环。逻辑有问题的,没有实现先写10个,再读10个的效果。一个循环是搞不定的。

import java.io.*;
public class Exam9_27 {

public static void main(String[] args)throws IOException {

RandomAccessFile rf = new RandomAccessFile("d:\\test.dat", "rw");
int a[] = new int[10];
for (int i = 0; i <10; i++) {
a[i] = (int) (Math.random() * 100);
rf.close();
rf = new RandomAccessFile("d:\\test.dat", "rw");
rf.seek(1*4);
rf.read();

System.out.println( rf.read());
rf.seek(9*4);
rf.read();
System.out.println("Value " + i + ": " + rf.read());
rf.close();

}

}

}

}

}