我正在尝试创建一个程序来获取 12 名学生的考试统计数据。在此程序中,输入文件由程序读取,然后打印到输出文件,但存在许多问题。该程序正在读取输入文件但不打印第一个 ID。无论我做什么,C 和 I 都不会打印在 More Right/Wrong 列下。此外,<10 will not print uder the Omit column either.here is my code.
import java.util.Scanner;
import java.io.*;
public class Topic3_HW3 {
public static void main(String[] args) throws IOException {
PrintWriter outputFile;
outputFile = new PrintWriter("HW3_output.txt");
// outputFile = new PrintWriter(System.out);
// TODO Auto-generated method stub
File myFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\Topic3_input.txt");
Scanner inputFile = new Scanner(myFile);
// print table header and columns
outputFile.printf("%40s", "\tTable of Grades");
outputFile.printf("\n%1s %7s %9s %12s %9s %10s %7s %12s %8s","\nStudent","ID","Correct","Incorrect","Total","More Right/Wrong","Omit","Grade","Percent");
outputFile.println();
// declare datatypes for different variables
double pct;
int grade, id, corr, incorr, total, omit;
int student = 0;
//prompt user to input initial student's ID
System.out.print("Enter student's ID(negative 1 to stop): ");
id = inputFile.nextInt();
while (id != -1) {
//prompt user to enter said student's wrong/right questions
System.out.print("\nEnter the amount of questions the student answered correctly: ");
corr = inputFile.nextInt();
System.out.print("\nEnter the amount of questions the student answered incorrectly: ");
incorr = inputFile.nextInt();
student++;
total = corr + incorr;
// print error if total is more than 50 questions
if (50 < total) {
System.out.println("ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.");}
System.out.print("\nEnter the desired student's ID(negative 1 to stop): ");
id = inputFile.nextInt();
omit = 50 - (corr + incorr);
grade = corr * 2;
// calculate percentage correct. type cast correct and total to make sure it
// doesnt truncate
pct = (double) corr / total;
//print if the omitted amount of questions is less, more, or equal to 10
if (omit < 10)
outputFile.printf("%-15s", "<10");
if (10 < omit)
outputFile.printf("%-15s", "10<");
if (omit == 10)
outputFile.printf("%-15s", "10");
if (corr<incorr)
outputFile.printf("%-10s","I");
if(incorr<corr)
outputFile.printf("%-10s","C");
if (corr==incorr)
outputFile.printf("%-10s","=");
//print values for corresponding columns
outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);
}
//notify viewer that program has run
System.out.println("\nThe program is finished.");
// flush buffer and close i/o files
outputFile.flush();
outputFile.close();
inputFile.close();
}
}
我尝试更改 Cs 和 Is 的 %-10 格式,但即使我执行 %-100,它仍然不会在百分比列值之前打印。我期待该表打印其相应列的完整值,包括输入文件的第 1 行。我还期待 Cs 和 Is 在更多正确/错误列和 10 下打印
无论我做什么,C 和 I 都不会打印在 More Right/Wrong 列下。
在处理像这样的格式化程序时,列结构在迭代中是相同的非常重要,例如......
outputFile.printf("\n%1s %7s %9s %12s %9s %10s %7s %12s %8s","\nStudent","ID","Correct","Incorrect","Total","More Right/Wrong","Omit","Grade","Percent");
//...
outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);
第一个标题列是1个字符宽,第二个是7,但是第二个输出列是14??
现在,考虑到
Student
无论如何都是 7 个字符宽,第一列的宽度没有意义。
相反,它“应该”看起来更像……
System.out.printf("\n%-7s %-4s %-7s %-9s %-5s %-16s %-4s %-6s %-8s", "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
//...
System.out.printf("\n%7d %4d %7d %9s %5s %16s %4d %6.3f %3.2f", student, id, corr, incorr, total, value, omit, grade, pct);
此外,<10 will not print uder the Omit column either.here is my code.
是的,这是因为您在打印任何其他信息之前打印了“省略”。虽然你“可以”这样做,但从可读性的角度来看,你应该在打印时将“省略”信息作为整个数据的一部分包括在内。
所以,我稍微修改了您的代码以修复列并将“省略”信息添加到最终数据行中...
File myFile = new File("marks.txt");
try (Scanner inputFile = new Scanner(myFile)) {
// print table header and columns
System.out.printf("%40s", "\tTable of Grades");
System.out.printf("\n%7s %4s %7s %10s %9s %16s %4s %5s %8s", "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
System.out.println();
int student = 0;
int id = inputFile.nextInt();
while (id != -1) {
int corr = inputFile.nextInt();
int incorr = inputFile.nextInt();
student++;
int total = corr + incorr;
// Not sure if this has anything to do with your current
// assignment, but it's just adding noise
//if (50 < total) {
// System.out.println("ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.");
//}
int omit = 50 - (corr + incorr);
double grade = corr * 2;
// calculate percentage correct. type cast correct and total to make sure it
// doesnt truncate
double pct = (double) corr / total;
//print if the omitted amount of questions is less, more, or equal to 10
String value = "";
if (omit < 10) {
value = "<10";
}
if (10 < omit) {
value = "10<";
}
if (omit == 10) {
value = "10";
}
if (corr < incorr) {
value = "I";
}
if (incorr < corr) {
value = "C";
}
if (corr == incorr) {
value = "=";
}
//print values for corresponding columns
//System.out.printf("\n%7s %4s %7s %10s %9s %16s %4s %5s %8s", "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
System.out.printf("\n%7d %4d %7d %10s %9s %16s %4d %5.3f %3.2f", student, id, corr, incorr, total, value, omit, grade, pct);
id = inputFile.nextInt();
}
//notify viewer that program has run
System.out.println("\nThe program is finished.");
// flush buffer and close i/o files
}
现在打印
Table of Grades
Student ID Correct Incorrect Total More Right/Wrong Omit Grade Percent
1 1234 10 11 21 I 29 20.000 0.48
2 1002 47 2 49 C 1 94.000 0.96
3 9000 31 19 50 C 0 62.000 0.62
4 8110 12 10 22 C 28 24.000 0.55
5 9062 40 0 40 C 10 80.000 1.00
6 9001 25 25 50 = 0 50.000 0.50
7 2764 1 49 50 I 0 2.000 0.02
8 1010 10 10 20 = 30 20.000 0.50
9 2324 19 30 49 I 1 38.000 0.39
10 1623 20 28 48 I 2 40.000 0.42
11 1423 20 30 50 I 0 40.000 0.40
12 6969 0 50 50 I 0 0.000 0.00
13 4200 41 6 47 C 3 82.000 0.87
The program is finished.
现在,如果您希望列右对齐,那么您可以在列宽前使用
-
,例如%-7d