急,求简单java源程序

2024-11-17 15:03:40
推荐回答(5个)
回答1:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Save {

public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
String str = "";
Scanner scan = new Scanner(System.in);
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(
"data.txt", true));
str = scan.nextLine();
buf.write(str);
buf.newLine();
buf.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
================================
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class Read {

public static void main(String[] args) {
try {
double sum = 0;
BufferedReader buf = new BufferedReader(new FileReader("data.txt"));
ArrayList list = new ArrayList();
String str = "";
while ((str = buf.readLine()) != null) {
list.add(Double.parseDouble(str));
}
for (int i = 0; i < list.size(); i++) {
sum = sum + list.get(i);
}
System.out.println("arr is " + sum / list.size());
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

============================================
因为writeInt方法返回的是以 4-byte 值。
api中是这样说的,将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。如果没有抛出异常,则计数器 written 增加 4。
所以你写入的是字节,而不是字符串。
个人认为用BufferedWriter和BufferedReader比较好

回答2:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
public class test {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Integer [] a = new Integer[5];
String [] chengji = new String[5];
Integer avgPoint =0; //平均分
Integer maxPoint =0; //最高分
int j=0;
for (int i = 0; i < 5; i++) {
a[i] = in.nextInt();
}
FileWriter writer = null;
FileReader reader = null;
BufferedReader readerBuff =null;

Integer b = 0;

try{
// write
writer=new FileWriter("d:/HW.txt");
writer.write(a[0].toString()+"\r\n");
writer.write(a[1].toString()+"\r\n");
writer.write(a[2].toString()+"\r\n");
writer.write(a[3].toString()+"\r\n");
writer.write(a[4].toString());
writer.close();
// read
reader =new FileReader("d:/HW.txt");
readerBuff = new BufferedReader(reader);
while((chengji[j]=readerBuff.readLine())!=null){
j++;
if(j==5){
break;
}
}
reader.close();
avgPoint=(Integer.parseInt(chengji[0])+Integer.parseInt(chengji[1])+Integer.parseInt(chengji[2])+Integer.parseInt(chengji[3])+Integer.parseInt(chengji[4]))/5;
for (int i=0;i if(Integer.parseInt(chengji[i]) > maxPoint){
maxPoint = Integer.parseInt(chengji[i]);
}
}
System.out.println(avgPoint);
System.out.print(maxPoint);
}catch(Exception e){
System.out.print(e.getMessage());
}

}
}

闲着没事写了个,希望对你有帮助,不懂可以问

回答3:

你自己先写点吧,就让别人帮你做,也不是办法!!你自己那不会了再在这问,我相信有很多人愿意帮你的!!!一点也不做也太没有诚意了!

回答4:


那位 四毛大哥
什么叫简单的你都不知道怎么写了
口气真大啊
不然你帮他写份难点的啊

怎么就觉得这人是在问作业的...

回答5:

麻烦一点。。但是比较简单。。你可以自己去网上找各功能程序然后拼接一下。。