我是java的新手,所以请忽略我明显的错误。 我有一个txt文件,其格式如下:
Student Name,Mathmatics_Marks,Physics_Marks,Chemistry_Marks,Biology_Marks
A,10,20,30,40
B,15,15,48,69
C,45,48,48,79
D,48,15,12,55
期望输出:
I need to do the following output format from the above txt files:
Student Name (Appended with "Student:" prefix)
Pass/Fail (Appended with "Exam Status:" prefix)
Average_Marks (Appended with "Average_Marks:" prefix)
Maximum_Marks(Appended with "Maximum_Marks:" prefix)
Minimum_Marks(Appended with "Minimum_Marks:" prefix)
For example:
Student: A
Exam Status: Pass
Average_Marks:25
Maximum_Marks:40
Minimum_Marks:10
<<so on for other students>>
...........................
...........................
...........................
...........................
所需输出的逻辑/算法:
1) If (Mathmatics_Marks+Physics_Marks+Chemistry_Marks,Biology_Marks)=>100 then Pass else Fail
2) Find out the average marks of student.
3) Write Maximum marks
4) Write Minimum marks
我的方法: 1-我可以使用以下代码将数据加载到ArrayList并从txt文件打印数据但无法实现所需的输出:
public static void main(String[] args)
{
//Input file path
String fileToParse = "C:\\Users\\DELL-PC\\Desktop\\Analysis.txt";
BufferedReader fileReader = null;
//Delimiter Declaration
final String DELIMITER = ",";
try
{
List<String> Student_log= new ArrayList<String>();
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileToParse));
//Reading the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
for(String token : tokens)
{
//Print all tokens
/// Array Initialization Part
System.out.println(token);
Student_log.add(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
请在我的代码中帮助我。
答案 0 :(得分:1)
您需要初始化一些变量来计算平均值,最小值,最大值等。然后在while循环中检索每行的值并进行计算。
Example Code:
while ((line = fileReader.readLine()) != null) {
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
name = tokens[0];
math = Integer.valueOf(tokens[1]);
physics =Integer.valueOf(tokens[2]);
chem = Integer.valueOf(tokens[3]);
bio = Integer.valueOf(tokens[4]);
if (math + physics + chem + bio > 100) {
pass = "Pass";
} else {
pass = "Fail";
}
System.out.println("Student: "+ name);
System.out.println("Exam Status: "+ pass);
}
答案 1 :(得分:1)
public static void main(String[] args)
{
//Input file path
String fileToParse = "C:\\Users\\DELL-PC\\Desktop\\Analysis.txt";
BufferedReader fileReader = null;
//Delimiter Declaration
final String DELIMITER = ",";
try
{
List<String> Student_log= new ArrayList<String>();
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileToParse));
//Reading the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
student_log = new ArrayList<>();
for(String token : tokens)
{
//Print all tokens
/// Array Initialization Part
Student_log.add(token);
}
// calculate average
int sum = 0;
for(int i=1; i<student_log.size();i++){
sum = sum + Integer.parseInt(student_log.get(i));
}
double average = sum/(student_log.size()-1);
String result = "fail";
if(average > 100){
result = "pass";
}
int max = Integer.parseInt(student_log.get(1));
//Calculate max
for(int i=1; i<student_log.size(); i++){
if(max < Integer.parseInt(student_log.get(i)))
max = Integer.parseInt(student_log.get(i));
}
//Calculate min
int min = Integer.parseInt(student_log.get(1));
//Calculate max
for(int i=1; i<student_log.size(); i++){
if(min > Integer.parseInt(student_log.get(i)))
min = Integer.parseInt(student_log.get(i));
}
System.out.println("Student: " + student_log.get(0));
System.out.println("Exam_status: " + result);
System.out.println("Average marks: " +average);
System.out.println("Maximum marks: " +max);
System.out.println("Minimum marks: " +min);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样可以正常工作但事实上,您将所有内容存储到字符串的数组列表中。我建议你创建一个类的模型,名称为字符串,所有其他标记为整数。这将是一个很好的编程实践,但是这是一个简单的程序,在这种情况下你可以使用Integer.parseInt()。
但是,我想在此提出一点意见。计算max和min值时,永远不要为初始max和min变量赋值0。通常很多人这样做。因为,有些情况会出现负面影响(不是在这种情况下)。但是,在这种情况下,如果我们将min指定为0,则永远不会输入循环。
答案 2 :(得分:0)
您可能遇到一些问题。首先,您需要一些计算来计算通过/失败,最大值,最小值等。因此,您需要首先遍历这些值以确定,转换为整数,依此类推。然后计算出的值是您要打印出来的值,但是手动添加一些其他文本,而不是在该循环中。所以一些Student_log.add(文字);声明,每个计算一个。也跳过第一行文字。
答案 3 :(得分:0)
您需要忽略第一行并为您的4个选项添加方法,例如
getAverageMark(List marks) {
//find average
}
其中List标记 - 特定学生的所有标记,您必须阅读每行