编写一个程序:要求(从标准输入中)输入五个学生的成绩(从0到100的整数),并将这五个数保存到文件“data.txt”中。
然后再编写一个程序:从文件“data.txt”中读取这五个学生的成绩,并计算它们平均数,然后按从小到大的顺序(在标准输出窗口)输出这五个学生的成绩。
然后再编写一个程序:从文件“data.txt”中读取这五个学生的成绩,并计算它们平均数,然后按从小到大的顺序(在标准输出窗口)输出这五个学生的成绩。
解决方案 »
- 谁知道ToStringBuilder的作用是什么呢
- Java初学dos配置
- Class 类的 asSubClass方法怎么使用
- 请问一个怎样把数据放进去数据库里面?错在哪里?
- 救命,关于java中终止线程问题
- 请一个在BS结构上调用客户端DLL的解决方案 分不够再加!!
- 求三个正则表达式
- 怎样在JCreator中能像JBuilder中那样单步调试?
- 求大神改改代码,小弟实在没有办法了,就是N个人站一圈,报数从1开始报到3的人退出。答案看过了,但是不清楚自己的哪里错了,求批改
- 求大神帮忙看不知道 没想明白哪出的问题JAVA
- 请问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
编译没有错 运行也没错 就是没有结果