我创建了具有学生分数的文件。我得到了每个测试的平均值,但现在我如何找到每个测试的最大值和最小值。我试过但它只是打印随机数字和想法?另外如果我想用新文件上的if else语句打印出他们的成绩我该如何做?
import java.io.File;
import java.io.IOException;
import java.util.*;
public class Project3 {
public static void main(String[] args) throws IOException {
ReadFile r = new ReadFile();
r.openFile();
r.readFile();
r.closeFile();
--
import java.io.*;
import java.lang.*;
import java.util.*;
public class Grades {
private Formatter x;
//Formatter variable- output string to file
public void openFile(){
//method to open file
try{
x = new Formatter("grades.txt");
}
catch(Exception e){
System.out.println("Could not find file.");
}
}
public void addRecords(){
x.format("%d",8);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", " Thui", "Bhu", 100, 90, 80, 100, 89, 99, 88);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n ArianaB", "Smith", 90, 90, 100, 100, 99, 100, 95);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Emily", "Gonzales", 100, 90, 100, 70, 78, 78, 80);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Jenifer", "L", 80, 90, 90, 100, 89, 99, 85);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Maria", "Jones", 65, 72, 77, 68, 62, 70, 65);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Bill", "Gates", 60, 54, 38, 62, 65, 60, 50);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Escobar", "Morris", 83, 77, 88, 76, 79, 72, 76 );
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Anne", "Latner", 80, 90, 85, 95, 90, 95, 90 );
}
public void closeFile(){
x.close();
}
//
}
--
import java.io.*;
import java.util.InputMismatchException;
import java.util.*;
public class ReadFile {
private Scanner x;
public void openFile(){
try{
x = new Scanner (new File ("grades.txt"));
}
catch(Exception e){
System.out.println("could not find file");
}}
ppublic void openFile(){
try{
x = new Scanner (new File ("grades.txt"));
}
catch(Exception e){
System.out.println("could not find file");
}}
public void createFile()throws IOException {
try{
File file = new File("grades.txt");
Scanner s = new Scanner(file);
int maxq1 =0, maxq2 =0, maxq3 =0, maxq4 =0;
int minq1 =100, minq2 =100, minq3 = 100, minq4 =100;
int proji_max=0, projii_max = 0, projiii_max =0;
int proji_min =100, projii_min = 100, projiii_min =100;
while(s.hasNext()){
{
double q1average = 0.0, q2average = 0.0, q3average = 0.0, q4average = 0.0;
double proji = 0.0, projii =0.0, projiii = 0.0;
double q1sum = 0.0, q2sum = 0.0, q3sum = 0.0, q4sum = 0.0;
double proji_sum = 0.0, projii_sum = 0.0, projiii_sum =0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
q1sum += Double.parseDouble(split[1]); // add all first grades
q1average++;
q2sum += Double.parseDouble(split[2]); // add all first grades
q2average++;
q3sum += Double.parseDouble(split[3]); // add all first grades
q3average++;
q4sum += Double.parseDouble(split[4]); // add all first grades
q4average++;
proji_sum += Double.parseDouble(split[5]); // add all first grades
proji++;
projii_sum += Double.parseDouble(split[6]); // add all first grades
projii++;
projiii_sum += Double.parseDouble(split[7]); // add all first grades
projiii++;
if(Double.parseDouble(split[1]) > maxq1)
maxq1 = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[2]) > maxq2)
maxq2 = (int) Double.parseDouble(split[2]);
if(Double.parseDouble(split[3]) > maxq3)
maxq3 = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[4]) > maxq4)
maxq4 = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[5]) > proji_max)
proji_max = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[6]) > projii_max)
projii_max = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[7]) > projiii_max)
projiii_max = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[1])< minq1)
minq1 = (int) Double.parseDouble(split[1]);
if(Double.parseDouble(split[2])< minq2)
minq2 = (int) Double.parseDouble(split[2]);
if(Double.parseDouble(split[3])< minq3)
minq3 = (int) Double.parseDouble(split[3]);
if(Double.parseDouble(split[4])< minq4)
minq4 = (int) Double.parseDouble(split[4]);
}
System.out.println(maxq2);
System.out.println(minq2);
double q1_average = (q1sum/q1average);
double q2_average = (q2sum / q2average);
double q3_average = (q3sum / q3average);
double q4_average = (q4sum / q4average);
double p1_average = (proji_sum/proji);
double p2_average = (projii_sum/projii);
double p3_average = (projiii_sum/projiii);
}
}}catch(Exception e){
e.printStackTrace();
}
}
public void closeFile(){
x.close();
}
}
答案 0 :(得分:1)
在while循环之前移动int maxq1 =0;
:此时您正在每个记录重置它...
答案 1 :(得分:1)
首先,每次重新初始化maxq1
为0,您需要移动到while
循环之前初始化的位置。
System.out.println(maxq1);
也需要在>>循环之后移动。