英文:
Output file not reading input file correctly, format out of order,
问题
我正在尝试创建一个程序,该程序接受12名学生的考试统计信息。在这个程序中,程序会读取输入文件,然后将其打印到输出文件,但存在一系列问题。程序能够读取输入文件,但无法打印第一个学生的ID。无论我怎么做,"C"和"I"都不会打印在"More Right/Wrong"列下面。此外,"<10"也无法打印在"Omit"列下面。以下是我的代码。
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");
File myFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\Topic3_input.txt");
Scanner inputFile = new Scanner(myFile);
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();
double pct;
int grade, id, corr, incorr, total, omit;
int student = 0;
System.out.print("Enter student's ID (negative 1 to stop): ");
id = inputFile.nextInt();
while (id != -1) {
System.out.print("\nEnter the number of questions the student answered correctly: ");
corr = inputFile.nextInt();
System.out.print("\nEnter the number of questions the student answered incorrectly: ");
incorr = inputFile.nextInt();
student++;
total = corr + incorr;
if (50 < total) {
System.out.println("ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.");
}
System.out.print("\nEnter the next student's ID (negative 1 to stop): ");
id = inputFile.nextInt();
omit = 50 - (corr + incorr);
grade = corr * 2;
pct = (double) corr / total;
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", "=");
outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);
}
System.out.println("\nThe program is finished.");
outputFile.flush();
outputFile.close();
inputFile.close();
}
}
我尝试更改了"C"和"I"的格式化,但即使我将它们改为"%-100s",它们仍然不会在百分比列的值之前打印。我期望表格能够打印出相应列的完整值,包括输入文件的第一行。我还期望"C"和"I"能够打印在"More Right/Wrong"列下,而"10<"则打印在"Omit"列旁边的数字下面。但实际上,列值打印得有些混乱。
希望这个解释对你有所帮助。
英文:
i am trying to create a program that takes exam statistics for 12 students. In this program the input file is read by the program and then printed to the output file, but there are a slew of issues. the program is reading the inputfile but not printing the first ID. No matter what I do, the C's and I's will not print under the More Right/Wrong column. Additionally, The <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();
}
}
I tried changing the %-10 formatting for the Cs and Is but even if I do %-100 it still will not print before the percent columns values. I was expecting the table to print the full values for their corresponding columns including line 1 of the input file. I was also expecting for the Cs and Is to print under More Right/Wrong column and the 10<s to print under the omit column next to the number.Instead, the column values are printing all over the place
答案1
得分: 1
I've translated the code parts as you requested. Here they are:
> No matter what I do, the C's and I's will not print under the More Right/Wrong column.
无论我做什么,C和I都不会打印在“More Right/Wrong”列下。
When dealing with formatters like this, it's REALLY important that the column structure is the same across iterations, for example...
当处理这样的格式化程序时,非常重要的一点是在各个迭代中列的结构保持一致,例如...
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);
The first title column is 1 character wide and the second is 7, but the second output column is 14??
第一个标题列宽度为1个字符,第二个为7个字符,但第二个输出列为14个字符吗?
Now, take into consideration that `Student` is 7 characters wide anyway and the first columns width doesn't make sense.
现在,请考虑一下,“Student”本身宽度就为7个字符,因此第一列的宽度没有意义。
Instead it "should" look something more like...
而更合理的方式应该如下...
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("%7d %4d %7d %9s %5s %16s %4d %6.3f %3.2f", student, id, corr, incorr, total, omit, grade, pct);
> Additionally, The <10 will not print under the Omit column either.
此外,"<10" 也不会在“Omit”列下打印,这是因为您在打印 "omit" 之前打印其他信息。从可读性的角度来看,最好将 "omit" 信息作为整个数据的一部分一起打印。
So, I modified your code slightly to fix the columns and add the "omit" information into the final data line...
因此,我稍微修改了您的代码,修复了列并将 "omit" 信息添加到最终的数据行中...
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 %-9s %-5s %-16s %-4s %-6s %-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;
int omit = 50 - (corr + incorr);
double grade = corr * 2;
double pct = (double) corr / total;
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 = "=";
}
System.out.printf("%7d %4d %7d %9s %5s %16s %4d %5.3f %3.2f", student, id, corr, incorr, total, value, omit, grade, pct);
id = inputFile.nextInt();
}
System.out.println("\nThe program is finished.");
}
This now prints the desired table format.
英文:
> No matter what I do, the C's and I's will not print under the More Right/Wrong column.
When dealing with formatters like this, it's REALLY important that the column structure is the same across iterations, for example...
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);
The first title column is 1 character wide and the second is 7, but the second output column is 14??
Now, take into consideration that Student
is 7 characters wide anyway and the first columns width doesn't make sense.
Instead it "should" look something more like...
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);
> Additionally, The <10 will not print uder the Omit column either.here is my code.
Yes, this is because you're printing the "omit" BEFORE you print any of the other information. While there is away you "could" do it like this, from a readability point of view, you should be including the "omit" information as part of the whole data when you print it.
So, I modified your code slightly to fix the columns and add the "omit" information in to the final data line...
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
}
This now prints
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.
Now, if you want the columns to be right aligned, then you can use -
before the column width, for example %-7d
> I still don't understand why line 1 is getting deleted from the table
Look at how you code is running...
id = inputFile.nextInt();
while (id != -1) {
//...
id = inputFile.nextInt();
//...
outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);
- You get the first
id
- You check for the end of file
- You load the various values
- You read the NEXT
id
- You print the values
... which id is getting printed?
There is a concept called "desk checking", where you take a pencil and paper and you walk through the program, updating the state as you go, it's a really good way to get you to think about how you code is actually running.
> and everything else is printing a line down
Look at...
outputFile.printf("\n%1s %7s %9s %12s %9s %10s %7s %12s %8s","\nStudent","ID","Correct","Incorrect","Total","More Right/Wrong","Omit","Grade","Percent");
You have two line breaks, one in the "format" and one in the values ("\nStudent"
)
Personally, I would only put one at the end of each output, for example...
System.out.printf("%40s", "\tTable of Grades\n");
System.out.printf("%-7s %-4s %-7s %-9s %-5s %-16s %-4s %-6s %-8s\n", "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
> Additionally we have to print the prompt as repeating instead of reading from the input file only
I'm sure you can figure that out, but it's otherwise just adding noise to the question
> there's a couple of things that I cant use yet like String value
🙄 That's one of the dangers of posting on SO, we'll answer the question usually in the most optimal way we know how.
Having said that, there is away to make it work, but sequence and order are important.
Start by printing the preceding information first...
System.out.printf("%7d %4d %7d %9s %5s", student, id, corr, incorr, total);
Then print the +/- information...
if (omit < 10) {
System.out.printf("%16s", "<10");
}
if (10 < omit) {
System.out.printf("%16s", "10<");
}
if (omit == 10) {
System.out.printf("%16s", "10");
}
if (corr < incorr) {
value = "I";
System.out.printf("%16s", "I");
}
if (incorr < corr) {
value = "C";
System.out.printf("%16s", "C");
}
if (corr == incorr) {
System.out.printf("%16s", "=");
}
Oh and this should be an if-elseif-else
statement, but I'm guessing you're not allowed to use those...
And then print the rest...
System.out.printf("%4d %6.3f %3.2f\n", omit, grade, pct);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论