这是一个关于DR.JAVA编程的一个问题 会的人帮我一下谢谢了
发布网友
发布时间:2024-04-03 21:42
我来回答
共2个回答
热心网友
时间:2024-07-25 06:35
加拿大。。。奥运会都没结束就这么急着开学的么。。。
题目没说输出到这里,我现在都打印在控制台上了。另外,你们老师说的TextIO我不知道是什么,我只是用常规的方法打开文件选择对话框
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Test {
public static void main(String[] args) {
Scanner scanner = null;
try {
File file = readUserSelectedFile();
if (file == null) return;
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
final int examNumber = 5;
int studentsNumber = scanner.nextInt();
int[] fullMark = new int[examNumber];
double[] ratios = new double[examNumber];
getNumbers(fullMark, scanner);
getNumbers(ratios, scanner);
for (int i = 0; i < examNumber; ++i) {
ratios[i] = ratios[i] * 1000 / fullMark[i];
}
int id, maxScoreID = -1, realScore, maxScore = -1;
int[] scores = new int[examNumber];
double score;
processOutputHeader();
for (int i = 0; i < studentsNumber; ++i) {
id = scanner.nextInt();
getNumbers(scores, scanner);
score = 0;
for (int j = 0; j < examNumber; ++j)
score += ratios[j] * scores[j];
realScore = processScore(id, score);
if (realScore > maxScore) {
maxScore = realScore;
maxScoreID = id;
}
}
System.out.printf("The best student is: %d with a mark of %s\n",
maxScoreID, parseScore(maxScore));
}
static String parseScore(int score) {
return (score / 10) + "." + (score % 10);
}
static void processOutputHeader() {
System.out.println("ID Nbr\tMark");
}
/*
* Since we need only one decimal, the full mark is considered to be 1000
* instead of 100 to eliminate the inaccuracy of floating point arithmetic.
* Otherwise, we might get a score of, say, 79.99. This score, through
* comparison with decimal literal 80, would be considered to be less than
* 80 and thus gets only one "+". However, it might be rounded to 80.0
* when displayed, which causes inconsistency.
*/
static int processScore(int id, double score) {
System.out.printf("%d\t", id);
int realScore = (int) Math.round(score);
int integer = realScore / 10, decimal = realScore % 10;
System.out.printf("%d.%d", integer, decimal);
if (integer >= 90) {
System.out.println("+++");
} else if (integer >= 80) {
System.out.println("++");
} else if (integer >= 70) {
System.out.println("+");
} else {
System.out.println();
}
return realScore;
}
static void getNumbers(int[] array, Scanner scanner) {
for (int i = 0; i < array.length; ++i)
array[i] = scanner.nextInt();
}
static void getNumbers(double[] array, Scanner scanner) {
for (int i = 0; i < array.length; ++i)
array[i] = scanner.nextDouble();
}
static File readUserSelectedFile() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter(null, "txt"));
chooser.setMultiSelectionEnabled(false);
chooser.showOpenDialog(null);
return chooser.getSelectedFile();
}
}
热心网友
时间:2024-07-25 06:37
你学计算机的话还是多动手吧!
引入下面这个包读取TEXT文档:
import java.util.TreeSet;
估计处理这些数字要用到正则表达式,
把数据用数组分别储存起来,
楼上这位已经给出了数据的计算方法了。
题目大意是:
编写一个JAVA程序:从一个数据文档中读取每个学生的得分信息,并且计算出最终成绩。该程序应该使用TextIO.readUserSelectedFile()方法从标准对话框中选择录入文件。文档中每一行包含一名学生的三项作业得分,一项测试得分,一项考试得分。
输入文件的格式如下:
文档的第一行包含一个表示该学生的代号;
第二行包含每项的满分标准;
第三行包含每项成绩的比重权值;
第四行及后面的所有行表示学生的各项对应成绩。
举例如下:
Here is the content of a sample input file:4
10 15 10 40 85
.1 .1 .1 .2 .5
902345 6 12 7 32 68
921147 7 11 8 29 71
993452 7 7 9 24 59
905466 8 13 8 34 74
在这个表中成绩计算方法是:
915466 (8/10*0.1+13/15*0.1+8/10*0.1+34/40*0.2+74/85*0.5)*100=85.2
输入要求计算出每个学生的最终成绩。90分以上的后面显示“+++”,80-90的显示“++”,70-80的显示“+”。程序末尾要说明最高成绩的学生的代号及其分值。
例如:
Id Nbr Mark
902345 77.0 +
921147 78.6 +
993452 67.4
905466 85.2 ++