我想要显示测验,实验室,实验室出勤,像这样的Midterms- 测验:66% 实验室:88% 实验室出勤率:81% 中期:91% 最终:不适用 总体平均值:85%。 但是我得到了
输出:
66.0
88.0
81.0
91.0
我如何获得正确的输出以实现我想要完成的任务。
import java.io.*;
import java.util.*;
public class FindGrade {
public static final int NUM_SCORE_TYPES = 5;
public static void main(String[] args) {
Scanner scan = null;
int[] quizArray = null;
int[] labArray = null;
int[] attendance = null;
int[] midterms = null;
int quizgrade = 0;
int labgrade = 0;
int attendance_1 = 0;
int midterms_1 = 0;
String name;
try {
scan = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
// each iteration is for single exam type (ie: Quizzes is the 1st one)
for (int i = 0; i < NUM_SCORE_TYPES; i++) {
name = scan.next();
int numScores = scan.nextInt();
int maxGrade = scan.nextInt();
if (name.equals("Quizzes")) {
quizArray = new int[numScores];
readScores(quizArray, numScores, scan,maxGrade);
} else if (name.equals("Labs")) {
labArray = new int[numScores];
readScores(labArray, numScores, scan,maxGrade);
} else if (name.equals("Lab_attendance")) {
attendance = new int[numScores];
readScores(attendance, numScores, scan,maxGrade);
} else if (name.equals("Midterms")) {
midterms = new int[numScores];
readScores(midterms, numScores, scan,maxGrade);
}
}
}
public static void readScores(int[] scoreArray, int numScores, Scanner scan, int maxGrade) {
for (int i = 0; i < numScores; i++) {
scoreArray[i] = scan.nextInt();
}
System.out.println(average(scoreArray, numScores, maxGrade));
}
public static int normalize(int[] scoreArray, int maxGrade) {
int total = 0;
for (int i = 0; i < scoreArray.length; i++) {
total += scoreArray[i];
}
int percent = Math.round(total * 100 / maxGrade);
return percent;
}
public static double average(double[] scoreArray, int numScores,int maxGrade) {
double sum = 0;
for (int i = 0; i < scoreArray.length; i++) {
sum += scoreArray[i];
}
double average = Math.round((sum / numScores)*100/maxGrade);
return average;
}
输入文件:
Quizzes 8 10
5 8 9 10 4 0 10 7
Labs 6 100
95 90 100 87 63 92
Lab_attendance 16 1
1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1
Midterms 2 100
87 94
Final 0 100
答案 0 :(得分:0)
您需要将名称传递给平均值并打印名称和分数。 通过建议查看固定代码下方,它提供您正在寻找的输入。
import java.io.*;
import java.util.*;
public class FindGrade {
public static final int NUM_SCORE_TYPES = 5;
public static void main(String[] args) {
Scanner scan = null;
int[] quizArray = null;
int[] labArray = null;
int[] attendance = null;
int[] midterms = null;
int quizgrade = 0;
int labgrade = 0;
int attendance_1 = 0;
int midterms_1 = 0;
String name;
try {
scan = new Scanner(new File("c:\\temp\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
// each iteration is for single exam type (ie: Quizzes is the 1st one)
for (int i = 0; i < NUM_SCORE_TYPES; i++) {
name = scan.next();
int numScores = scan.nextInt();
int maxGrade = scan.nextInt();
if (name.equals("Quizzes")) {
quizArray = new int[numScores];
readScores(quizArray, numScores, scan,maxGrade, "Quizzes");
} else if (name.equals("Labs")) {
labArray = new int[numScores];
readScores(labArray, numScores, scan,maxGrade, "Labs");
} else if (name.equals("Lab_attendance")) {
attendance = new int[numScores];
readScores(attendance, numScores, scan,maxGrade, "Lab_attendance");
} else if (name.equals("Midterms")) {
midterms = new int[numScores];
readScores(midterms, numScores, scan,maxGrade, "Midterms");
}
}
}
public static void readScores(int[] scoreArray, int numScores, Scanner scan, int maxGrade, String name) {
for (int i = 0; i < numScores; i++) {
scoreArray[i] = scan.nextInt();
}
average(scoreArray, numScores, maxGrade, name);
}
public static int normalize(int[] scoreArray, int maxGrade) {
int total = 0;
for (int i = 0; i < scoreArray.length; i++) {
total += scoreArray[i];
}
int percent = Math.round(total * 100 / maxGrade);
return percent;
}
public static double average(int[] scoreArray, int numScores,int maxGrade, String name) {
double sum = 0;
for (int i = 0; i < scoreArray.length; i++) {
sum += scoreArray[i];
}
double average = Math.round((sum / numScores)*100/maxGrade);
System.out.println( name + ":" + average);
return average;
}
}
然后在运行时预期输出:
Quizzes:66.0
Labs:88.0
Lab_attendance:81.0
Midterms:91.0
答案 1 :(得分:0)
这是您修改过的代码&amp;预期o / p:
import java.io.*;
import java.util.*;
public class FindGrade {
public static final int NUM_SCORE_TYPES = 5;
public static void main(String[] args) {
Scanner scan = null;
String quizzes= "Quizzes";
String labs= "Labs";
String lab_attendance= "Lab_attendance";
String midterm= "Midterms";
int[] quizArray = null;
int[] labArray = null;
int[] attendance = null;
int[] midterms = null;
int quizgrade = 0;
int labgrade = 0;
int attendance_1 = 0;
int midterms_1 = 0;
String name;
try {
scan = new Scanner(new File("C://Users//Desktop//input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
// each iteration is for single exam type (ie: Quizzes is the 1st one)
for (int i = 0; i < NUM_SCORE_TYPES; i++) {
name = scan.next();
int numScores = scan.nextInt();
int maxGrade = scan.nextInt();
if (name.equals("Quizzes")) {
quizArray = new int[numScores];
readScores(quizArray, numScores, scan,maxGrade, quizzes);
} else if (name.equals("Labs")) {
labArray = new int[numScores];
readScores(labArray, numScores, scan,maxGrade, labs);
} else if (name.equals("Lab_attendance")) {
attendance = new int[numScores];
readScores(attendance, numScores, scan,maxGrade,lab_attendance );
} else if (name.equals("Midterms")) {
midterms = new int[numScores];
readScores(midterms, numScores, scan,maxGrade, midterm);
}
}
}
public static void readScores(int[] scoreArray, int numScores, Scanner scan, int maxGrade, String name) {
for (int i = 0; i < numScores; i++) {
scoreArray[i] = scan.nextInt();
}
average(scoreArray, numScores, maxGrade, name);
}
public static int normalize(int[] scoreArray, int maxGrade) {
int total = 0;
for (int i = 0; i < scoreArray.length; i++) {
total += scoreArray[i];
}
int percent = Math.round(total * 100 / maxGrade);
return percent;
}
public static double average(int[] scoreArray, int numScores,int maxGrade, String name) {
double sum = 0;
for (int i = 0; i < scoreArray.length; i++) {
sum += scoreArray[i];
}
double average = Math.round((sum / numScores)*100/maxGrade);
System.out.println( name + ":" + average+" %");
return average;
}
}
结果是:
Quizzes:66.0 %
Labs:88.0 %
Lab_attendance:81.0 %
Midterms:91.0 %**
答案 2 :(得分:-2)
您实际上并没有发布任何代码来打印任何,但您想要的可能是System.out.format()
。