[求助]有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩),计算出总成绩,并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩),计算出总成绩,并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;class Score{ String[] name = new String[5]; double[] score = new double[5]; double[] sum = new double[5]; public void input() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean change =true; while(change){ try{ for(int i = 0;i <= 4;i++){ System.out.print("Input the name : "); name[i] = br.readLine(); for(int j = 0;j <= 2;j++){ System.out.print("Input "+(j+1)+" score : "); score[j] = Integer.parseInt(br.readLine()); } System.out.println(); sum[i] = score[0]+score[1]+score[2]; } change =false; } catch(NumberFormatException nfe){ System.out.println("Sorry,The Score only can input a number,Please input again."); } } } public void compositor() throws IOException{ FileWriter fw = new FileWriter("F://Socre.txt"); //Îļþ·ÅÓÚfÅÌÏ£¬ÎļþÃûΪ"Socre.txt". BufferedWriter bw = new BufferedWriter(fw); double temp1; String temp2; for(int i = 0;i < 4;i++){ for(int j = 1;j < 5-i;j++){ if(sum[j-1] > sum[j]){ temp1 = sum[j]; sum [j] = sum[j-1]; sum[j-1] = temp1; temp2 = name[j]; name[j] = name[j-1]; name[j-1] = temp2; } } } for(int i = 0;i <= 4;i++){ bw.write(name[4-i]); bw.write(" "+sum[4-i]); bw.newLine(); } bw.close(); } public static void main(String[] args) throws IOException{ Score s = new Score(); s.input(); s.compositor(); }} 对不起,文件名没看清楚.import java.io.*;class Score{ String[] name = new String[5]; double[] score = new double[5]; double[] sum = new double[5]; public void input() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean change =true; while(change){ try{ for(int i = 0;i <= 4;i++){ System.out.print("Input the name : "); name[i] = br.readLine(); for(int j = 0;j <= 2;j++){ System.out.print("Input "+(j+1)+" score : "); score[j] = Integer.parseInt(br.readLine()); } System.out.println(); sum[i] = score[0]+score[1]+score[2]; } change =false; } catch(NumberFormatException nfe){ System.out.println("Sorry,The Score only can input a number,Please input again."); } } } public void compositor() throws IOException{ FileWriter fw = new FileWriter("F://stud.txt"); //文件存放于f盘下的"stud.txt"文件下; BufferedWriter bw = new BufferedWriter(fw); double temp1; String temp2; for(int i = 0;i < 4;i++){ for(int j = 1;j < 5-i;j++){ if(sum[j-1] > sum[j]){ temp1 = sum[j]; sum [j] = sum[j-1]; sum[j-1] = temp1; temp2 = name[j]; name[j] = name[j-1]; name[j-1] = temp2; } } } for(int i = 0;i <= 4;i++){ bw.write(name[4-i]); bw.write(" "+sum[4-i]); bw.newLine(); } bw.close(); } public static void main(String[] args) throws IOException{ Score s = new Score(); s.input(); s.compositor(); }} 而且在class前面加上public,晚上做有点累了,所以有点急,嘿嘿,想睡觉 我也是初学者,今天刚想出来的一个简单的程序。你参考参考。写的不好import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.util.Scanner;public class Demo50 { /** * 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩) * ,计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请依次输入学生号,姓名,三门课成绩,用逗号隔开,每个学生之间用句号隔开"); String str = input.next(); String[] student = str.split("\\."); BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\stud.txt")); for (int i = 0; i < student.length; i++) { String[] scores = student[i].split("\\,"); bw.write(student[i]); int[] num1 = new int[3]; int sum = 0; for (int m = 0; m < 3; m++) { num1[m] = Integer.parseInt(scores[m + 2]); sum += num1[m]; } bw.write(".平均分:" + sum/3); System.out.println(sum/3); } bw.flush(); bw.close(); }} bw.write(".平均分:" + sum/3);这里会丢失精度,算出来肯定是个整数。比如211/3=70;小数部分丢了 int sum = 0;这里定义为:double sum ; CGLIB动态代理生成问题 问一个算法. 正则表达式 判断字符串第一次出现 请大家推荐一些JAVA入门的书,我学过C。谢谢 怎样用java在xml中插入数据啊? 高分求取JSP函数大全,邮箱:[email protected],谢谢谢谢:) 菜鸟问题~ 请教关于在JTree中加两个图标的问题 关于接口、抽像类在使用上的疑问 关于用ftp和com做远程数据传输的程序。要求能够在远程的不同数据库之间传输数据,以及传输文件,如excel和txt文件。 关于selectionKey.iswritable()这方法的问题 eclipse 与 JDK 的区别
import java.io.*;class Score{
String[] name = new String[5];
double[] score = new double[5];
double[] sum = new double[5];
public void input() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean change =true;
while(change){
try{
for(int i = 0;i <= 4;i++){
System.out.print("Input the name : ");
name[i] = br.readLine();
for(int j = 0;j <= 2;j++){
System.out.print("Input "+(j+1)+" score : ");
score[j] = Integer.parseInt(br.readLine());
}
System.out.println();
sum[i] = score[0]+score[1]+score[2];
}
change =false;
}
catch(NumberFormatException nfe){
System.out.println("Sorry,The Score only can input a number,Please input again.");
}
}
}
public void compositor() throws IOException{
FileWriter fw = new FileWriter("F://Socre.txt"); //Îļþ·ÅÓÚfÅÌÏ£¬ÎļþÃûΪ"Socre.txt".
BufferedWriter bw = new BufferedWriter(fw);
double temp1;
String temp2;
for(int i = 0;i < 4;i++){
for(int j = 1;j < 5-i;j++){
if(sum[j-1] > sum[j]){
temp1 = sum[j];
sum [j] = sum[j-1];
sum[j-1] = temp1;
temp2 = name[j];
name[j] = name[j-1];
name[j-1] = temp2;
}
}
}
for(int i = 0;i <= 4;i++){
bw.write(name[4-i]);
bw.write(" "+sum[4-i]);
bw.newLine();
}
bw.close();
}
public static void main(String[] args) throws IOException{
Score s = new Score();
s.input();
s.compositor();
}
}
String[] name = new String[5];
double[] score = new double[5];
double[] sum = new double[5];
public void input() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean change =true;
while(change){
try{
for(int i = 0;i <= 4;i++){
System.out.print("Input the name : ");
name[i] = br.readLine();
for(int j = 0;j <= 2;j++){
System.out.print("Input "+(j+1)+" score : ");
score[j] = Integer.parseInt(br.readLine());
}
System.out.println();
sum[i] = score[0]+score[1]+score[2];
}
change =false;
}
catch(NumberFormatException nfe){
System.out.println("Sorry,The Score only can input a number,Please input again.");
}
}
}
public void compositor() throws IOException{
FileWriter fw = new FileWriter("F://stud.txt"); //文件存放于f盘下的"stud.txt"文件下;
BufferedWriter bw = new BufferedWriter(fw);
double temp1;
String temp2;
for(int i = 0;i < 4;i++){
for(int j = 1;j < 5-i;j++){
if(sum[j-1] > sum[j]){
temp1 = sum[j];
sum [j] = sum[j-1];
sum[j-1] = temp1;
temp2 = name[j];
name[j] = name[j-1];
name[j-1] = temp2;
}
}
}
for(int i = 0;i <= 4;i++){
bw.write(name[4-i]);
bw.write(" "+sum[4-i]);
bw.newLine();
}
bw.close();
}
public static void main(String[] args) throws IOException{
Score s = new Score();
s.input();
s.compositor();
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;public class Demo50 { /**
* 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩)
* ,计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请依次输入学生号,姓名,三门课成绩,用逗号隔开,每个学生之间用句号隔开");
String str = input.next();
String[] student = str.split("\\.");
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\stud.txt"));
for (int i = 0; i < student.length; i++) {
String[] scores = student[i].split("\\,");
bw.write(student[i]);
int[] num1 = new int[3];
int sum = 0;
for (int m = 0; m < 3; m++) {
num1[m] = Integer.parseInt(scores[m + 2]);
sum += num1[m];
}
bw.write(".平均分:" + sum/3);
System.out.println(sum/3);
}
bw.flush();
bw.close();
}}
这里会丢失精度,算出来肯定是个整数。比如211/3=70;小数部分丢了
这里定义为:double sum ;