编写一个程序:要求(从标准输入中)输入五个学生的成绩(从0到100的整数),并将这五个数保存到文件“data.txt”中。
然后再编写一个程序:从文件“data.txt”中读取这五个学生的成绩,并计算它们平均数,然后按从小到大的顺序(在标准输出窗口)输出这五个学生的成绩。
然后再编写一个程序:从文件“data.txt”中读取这五个学生的成绩,并计算它们平均数,然后按从小到大的顺序(在标准输出窗口)输出这五个学生的成绩。
解决方案 »
- java中怎样判断一个文件是否被打开?
- 用JScrollPane做的窗口 滚动条延时
- 一个用jdbc连接sql server 2005发生异常的问题
- (100分)关于纸牌游戏的几个问题。非常急,摆脱各位了!!!
- java 和 exchange server通信问题
- java nio中,链接中断时的内存泄漏问题
- 修改Look And Feel 后如何使JFrame和ContentPanel面板上的控件更新
- 请教各位大哥,这个问题怎么解决?
- 如何把上面这个16进制数转化成2个byte长度的数组????
- 谁能告诉我怎样用JDBC与SQL-SERVER7.0连接。
- 请问API文档怎么下载啊?
- JAVA IO 流的一个小问题,各位大大帮我看看
{
int countOfStudent=5;
try {
BufferedWriter out = new BufferedWriter(new FileWriter("data.txt"));
BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in)); for(int i=0;i<countOfStudent;i++){
out.write(Float.parseFloat(keyboard.readLine().trim()));
out.write("\n");
}
out.close();
} catch (IOException e) {
//IOException happened!
System.out.println("Exception was thrown when saving into file!");
}
}
{
int countOfStudent=5,i=0;
float score[]=new float[countOfStudent];
try {
BufferedReader in = new BufferedReader(new FileReader("company.record")); while ((str=in.readLine()) != null&&i++<countOfStudent) {
score[i] = Float.parseFloat(in.readLine().trim());
}
in.close();
} catch (IOException e) {
//IOException happened!
System.out.println("Exception was thrown when reading from file!");
}
//求平均值,排序我就不写了~
.....
}
out.write(""+Float.parseFloat(keyboard.readLine().trim()));
或者用:
out.write(keyboard.readLine().trim());
也可以,如果保证你输入的是合法数字的话;不合法上面那种会抛出异常~
public static void readScore()
{
int countOfStudent=5,i=0;
float score[]=new float[countOfStudent];
try {
BufferedReader in = new BufferedReader(new FileReader("data.txt")); while ((str=in.readLine()) != null&&i++<countOfStudent) {
score[i] = Float.parseFloat(str.trim());
}
in.close();
} catch (IOException e) {
//IOException happened!
System.out.println("Exception was thrown when reading from file!");
}
//求平均值,排序我就不写了~
.....
}
//J_Test013.java
import java.io.*;
public class J_Test013
{
public static void main(String args[])
{
String currentLine[]=new String[5];
int s[]=new int[5];
for (int i=0; i < args.length; i++)
{
System.err.println("Contents of file: " + args[i] + " are:");
try
{
LineNumberReader br =
new LineNumberReader(new FileReader(args[i]));
int j=0;
while (br.readLine() != null)
currentLine[j] = br.readLine();
s[j]=Integer.parseInt(currentLine[j]);
j++;
int m=0;
for(j=0;j<5;j++)
for(int k=0;k<5;k++)
{
if(s[k]>s[k+1])
m=s[k];
s[k]=s[k+1];
s[k+1]=m;
}
// currentLine[i]=String.valueOf(s[i]);
for(j=0;j<5;j++)
{
currentLine[i]=String.valueOf(s[i]);
System.out.println(br.getLineNumber() + ": " + currentLine[i]);
}
}
catch (IOException e)
{
System.err.println("Error: " + e);
}
}
}
} // End of class: J_Test013
编译没有错 运行也没错 就是没有结果