好吧所以我正在创建一个程序,根据用户输入的内容进行组织和显示。用户将加载文件,然后使用这些命令。用户只能加载具有逗号分隔值文本文件的文件。第一行将包含单词" name",后跟类中所有赋值的名称,以逗号分隔。第二行将给出类的名称,然后是每个赋值可能的点数。之后的任何行将具有学生姓名,后跟每个作业获得的分数。
For example:
name,essay 1,test 1,essay 2,test 2,final
Philosophy 101,5,20,5,20,50
Aristotle,4,18,3,15,40
Euclid,3,15,2,10,35
Immanuel Kant,4,18,4,20,48
Ben Franklin,5,20,4,19,49
程序将以"命令"开始。模式允许用户键入各种命令并从系统中获取结果。命令是:
1. exit
Causes the program to exit.
2. help
Causes the program to print a list of commands accepted by the system.
3. load [filename]
Causes the program to load a class database specified in the file [filename]. If a database is
currently loaded, the program should discarded it and replace it with the new one.
4. students
Prints out a list of students from the class, along with total points, and final grades (A, B, C, D,
F) for each student. These should be properly aligned in columns, so that the grades line up
above one another, and the student names are left aligned.
5. assignments
Prints out a list of assignments from the file, along with how many points are possible for each
assignment. As with students, the assignments should be properly aligned on columns.
6. student [student name]
Prints a report for the student. If the student does not exist, it prints a message stating that the
student does not exist, and continues without crashing. The report should contain the following
information:
1. Student name
2. A list of all assignments, and the grades the student achieved for them and points possible
3. Overall points made in the course and total points possible.
4. Final letter grade for the student, given the grading scale
A >= 90%
B >= 80%, < 90%
C >= 70%, < 80%
D >= 60%, < 70%
F < 60%
5. The report should be formatted reasonably, so that assignments and grades line up in
columns, for example.
7. assignment [assignment name]
Prints a report about a particular assignment, which includes the following information:
1. The assignment name, and points possible
2. The low, high, and average for the assignment
3. How many students achieved different grades (A-F) for the assignment.
8. grades
Prints a report about overall grades in the class. The report must include the following
information:
1. low, high, and average percentages in the class
2. how many students achieved the different grades in the class (A-F)
现在我感到困惑的是Load类。它基本上决定了其他类的信息,但我没有太多运气让它运行起来。到目前为止,这是我的所有代码。
import java.util.*;
import java.io.*;
public class program7
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Grade Stats by ");
System.out.print(">");
while(scan.hasNextLine())
{
String input = scan.nextLine();
if(input.equals("exit"))
{
System.exit(0);
}
else if(input.equals("help"))
{
System.out.println("exit - program closes.");
System.out.println("load [filename] - loads a class database specified in [filename].");
System.out.println("students - prints out a list of students from the class, along ");
System.out.println(" with total points, and final grades for each student.");
System.out.println("assignments - prints out a list of assignments from the file, along with points possible");
System.out.println("student [student name] - Prints report for the student");
System.out.print(">");
}
else if(input.contains("load"))
{
String[] split = input.split(" ");
Load load = new Load(split[1]);
System.out.print(">");
}
else if(input.equals("students"))
{
System.out.print(">");
}
else if(input.equals("assignments"))
{
System.out.print(">");
}
else if(input.contains("student"))
{
String[] split = input.split(" ");
Student student = new Student(split[1]);
System.out.print(">");
}
else if(input.contains("assignment"))
{
}
else if(input.equals("grades"))
{
}
else
{
System.out.println("exit - program closes.");
System.out.println("load [filename] - loads a class database specified in [filename].");
System.out.println("students - prints out a list of students from the class, along ");
System.out.println(" with total points, and final grades for each student.");
System.out.println("assignments - prints out a list of assignments from the file, along with points possible");
System.out.println("student [student name] - Prints report for the student");
System.out.print(">");
}
}
}
}
这是我的主要课程顺便说一句。以下是其他课程。
import java.util.*;
import java.io.*;
public class Load
{
public String inputFile;
public List<String> info = new ArrayList<String>();
public Load(String inputFile)
{
this.inputFile = inputFile;
}
public void getFileContent()
{
try
{
BufferedReader in = new BufferedReader(new FileReader(inputFile));
try
{
String line = "";
while(in.readLine() != null)
{
line = in.readLine();
info.add(line);
}
}
catch(IOException e)
{
System.err.println("Exception, man");
}
finally
{
in.close();
}
}
catch(FileNotFoundException e)
{
System.err.println("File wasnt found bro");
}
catch(IOException e)
{
System.err.println("Exception, man");
}
}
}
这是我的Load课程,以下是我的学生课程
public class Student
{
public Student()
{
}
}
我无法让Load工作,所以我没有搞砸这些。这是学生班。
import java.util.*;
import java.io.*;
public class Student
{
private String student;
public Student(String student)
{
this.student = student;
}
public String Splitter()
{
return "";
}
public String Printer()
{
return "";
}
}
现在我明白我必须有一个构造函数并通过在主类中创建它的实例来调用该类,但它似乎并没有在我的Load类中运行我的void方法。有没有办法解决这个问题?如果有任何更好的想法我喜欢听他们,我只是想想一种方法,我可以轻松地将信息(用户inputFile)传递给我的所有类,以便他们可以在用户的意愿,或根据他或她的输入。 哦,是的,这个程序只是在命令行上运行。
答案 0 :(得分:1)
看起来你从不在main中调用getFileContent()。应该看起来像这样:
else if(input.contains("load"))
{
String[] split = input.split(" ");
Load load = new Load(split[1]);
load.getFileContent(); // forgot to call
System.out.print(">");
}
如果提供的文件有效,则应该在Load类中指定的ArrayList中添加内容。