Java题目产生15个20~9999之间的随机整数,然后利用BufferedWriter类将其写入文件file2.txt中;
产生15个20~9999之间的随机整数,然后利用BufferedWriter类将其写入文件file2.txt中;之后再读取该文件中的数据并将它们以升序排序。我成功地做出随...
产生15个20~9999之间的随机整数,然后利用BufferedWriter类将其写入文件file2.txt中;之后再读取该文件中的数据并将它们以升序排序。我成功地做出随机数并写入文件,但是文件是一堆15个?;然后读取出来时还有更大的数。package promise;import java.util.*;import java.io.*;public class subject10 { public static void main(String[] args) throws IOException { BufferedWriter ri=new BufferedWriter(new FileWriter("d:\\cgj\\file2.txt")); Random ra=new Random(); int[] rr=new int[15]; for(int i=0;i<15;i++) { rr[i]=ra.nextInt(9979)+20; System.out.println(rr[i]); ri.write(rr[i]); ri.newLine(); } ri.flush(); System.out.println("------------------------------"); ri.close(); FileInputStream fin=new FileInputStream(("d:\\cgj\\file2.txt")); DataInputStream din=new DataInputStream(fin); try { for(int i=0;i<15;i++) { rr[i]=din.readInt(); } } catch(IOException e) {} din.close(); int temp; for(int i=0;i<14;i++) { if(rr[i]>rr[i+1]) { temp=rr[i]; rr[i]=rr[i+1]; rr[i+1]=temp; } } for(int ea:rr) System.out.println(ea); } }
展开
展开全部
import java.util.*;
import java.io.*;
public class subject10 {
public static void main(String[] args) throws IOException {
BufferedWriter ri = new BufferedWriter(new FileWriter("d:\\cgj\\file2.txt"));
Random ra = new Random();
int[] rr = new int[15];
for (int i = 0; i < 15; i++) {
rr[i] = ra.nextInt(9979) + 20;
System.out.println(rr[i]);
ri.write(String.valueOf(rr[i]));
ri.newLine();
}
ri.flush();
System.out.println("------------------------------");
ri.close();
FileInputStream fin = new FileInputStream(("d:\\cgj\\file2.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
try {
for (int i = 0; i < 15; i++) {
rr[i] = Integer.parseInt(br.readLine());
}
} catch (IOException e) {
}
br.close();
int temp;
for (int i = 0; i < 14; i++) {
if (rr[i] > rr[i + 1]) {
temp = rr[i];
rr[i] = rr[i + 1];
rr[i + 1] = temp;
}
}
for (int ea : rr)
System.out.println(ea);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |