我想知道是否有人有时间/耐心帮助我解决这个问题。 基本上我所要做的就是创建一个非常简单的学生数据库系统,它似乎并没有为我工作。这是我的主班,学生班和科目班。
科目类
import java.util.ArrayList;//ArrayList Import.
public class Subjects
{
/*(Public Variables)*/
public static String subjectName;
public static String subjectTutor;
public static ArrayList<Student> studentList = new ArrayList<Student>();
public static ArrayList<Student> mathsList = new ArrayList<Student>();
public static ArrayList<Student> excelList = new ArrayList<Student>();
public static ArrayList<Student> javaList = new ArrayList<Student>();
public static ArrayList<Student>classList= new ArrayList<Student>();
public Subjects(String sName)//If statement that will select the correct
tutor for the class
{
subjectName = sName;
if (subjectName == "maths")
{
subjectTutor= "Jennifer";
}
if (subjectName == "excel")
{
subjectTutor= "Ed";
}
if (subjectName == "java")
{
subjectTutor = "Brendan";
}
}
public static void printSubjectDetails()//Print Subject Details Method//Print the subject details listed below.
{
System.out.println("Subjects are " + subjectName + " and tutor is " + subjectTutor);
}
public static void printStudentList()
{
}
//Method for adding students to different classes
public static void addMathsStudent(Student localStudent)
{
mathsList.add(localStudent);
}
public static void addexcelStudent(Student localStudent)
{
excelList.add(localStudent);
}
public static void addStudent(Student localStudent)
{
studentList.add(localStudent);
}
public static void addjavaStudent(Student localStudent)
{
javaList.add(localStudent);
}
public static void printClass()//Print method that prints out the selected class when called in the main method.
{
System.out.println(subjectName + " Class List");
for(Student i : studentList)
{
Student.printStudent();
}
}
}
学生班
import java.util.Scanner;//Scanner Import.
public class Student
{
//variables
public static String studentFName;
public static String studentLName;
int studentGrade;
public Student()
{
Scanner input = new Scanner(System.in);
//Operations for student class.
System.out.println("Please enter First Name.");
studentFName = input.next();
System.out.println("Please enter student Surname.");
studentLName = input.next();
}
//Print operations for student class.
public static void printStudent()
{
System.out.println("Students First Name is:" + studentFName);
System.out.println("Students Surname is:" + studentLName);
}
}
主类
import java.util.Scanner;
public class AppMenu {
// An Auto-generated method stub. (created when making the class.)
public static void main(String[] args)
{
appmenu();
}
public static <printClass> void appmenu()
{
Scanner input = new Scanner(System.in);
System.out.println(">>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<");
System.out.println("Enter [.1] to enroll a student");
System.out.println("Enter [.2] to View Class List");
System.out.println("Enter [.3] to Remove Students");
System.out.println("Enter [.4] to Clear Class Lists");
System.out.println("Enter [.5] to Exit The App");
System.out.println(">>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<");
//Case and Break.
String choice = input.next();
{
switch(choice)
{
case "1":
Student s1 = new Student();
System.out.println("What class would you like to enroll in?");
System.out.println("Press [.1] Enroll In Maths");
System.out.println("Press [.2] Enroll In Excel");
System.out.println("Press [.3] Enroll In Java");
System.out.println("Press [.4] Enroll In All of the above");
choice = input.next();
switch(choice)
{
case "1":
Subjects.addMathsStudent(s1);
appmenu();
break;
case "2":
Subjects.addexcelStudent(s1);
appmenu();
break;
case "3":
Subjects.addjavaStudent(s1);
appmenu();
break;
case "4":
Subjects.addStudent(s1);
appmenu();
}
case "2":
System.out.println("Press [.1] to View Maths Class List");
System.out.println("Press [.2] to View Excel Class List");
System.out.println("Press [.3] to View Java Class List");
choice = input.next();
switch(choice)
{
case "1":
Subjects.printSubjectDetails();
appmenu();
break;
case "2":
Subjects.printSubjectDetails();
appmenu();
break;
case "3":
Subjects.printSubjectDetails();
appmenu();
break;
case "4":
Subjects.printSubjectDetails();
appmenu();
break;
}
case "3":
System.out.println("Which student would you like to remove ");
Subjects.printStudentList();
Subjects.studentList();
}
}
}
}
答案 0 :(得分:6)
如果不运行代码,您的第一个问题就在这里......
public Subjects(String sName) {
subjectName = sName;
if (subjectName == "maths") {
subjectTutor = "Jennifer";
}
if (subjectName == "excel") {
subjectTutor = "Ed";
}
if (subjectName == "java") {
subjectTutor = "Brendan";
}
}
Java中的{p> String
比较不是使用==
完成的,而是使用String#equals
方法进行的,这意味着它应该成为......
public Subjects(String sName) {
subjectName = sName;
if ("maths".equals(subjectName)) {
subjectTutor = "Jennifer";
}
if ("excel".equals(subjectName)) {
subjectTutor = "Ed";
}
if ("java".equals(subjectName)) {
subjectTutor = "Brendan";
}
}
我不确定这是假设Subjects.studentList();
是什么,但是我不鼓励你试图直接访问对象/类的字段并依赖适当的管理方法
此外,
switch (choice) {
case "1":
//...
case "2":
//...
case "3":
其中没有break
,这意味着如果choice
为1
,则案例2和3也会被执行......
不是依赖static
,而是为您要管理的每个主题创建一个Subject
课程,并将学生/导师添加到Subject
的每个实例中,这将使管理更多从长远来看更容易。
例证......
public class Student
{
//variables
public static String studentFName;
public static String studentLName;
这意味着无论你创建了多少Student
个实例,它们都会有相同的名字和姓氏......
考虑使用某种循环来管理菜单菜单而不是连续调用appmenu
,虽然这需要很长时间,但最终会出现堆栈溢出异常......
使用可运行示例进行更新
这是我在寻找其他问题时想出的代码......
import java.util.ArrayList;
import java.util.Scanner;
public class AppMenu {
public static void main(String[] args) {
appmenu();
}
public static <printClass> void appmenu() {
Scanner input = new Scanner(System.in);
Subject maths = new Subject("Maths", "Jennifer");
Subject excel = new Subject("Excel", "Ed");
Subject java = new Subject("Java", "Brendan");
String choice = null;
do {
System.out.println(">>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<");
System.out.println("Enter [.1] to enroll a student");
System.out.println("Enter [.2] to View Class List");
System.out.println("Enter [.3] to Remove Students");
System.out.println("Enter [.4] to Clear Class Lists");
System.out.println("Enter [.5] to Exit The App");
System.out.println(">>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<");
//Case and Break.
choice = input.next();
{
switch (choice) {
case "1":
Student s1 = new Student();
System.out.println("What class would you like to enroll in?");
System.out.println("Press [.1] Enroll In Maths");
System.out.println("Press [.2] Enroll In Excel");
System.out.println("Press [.3] Enroll In Java");
System.out.println("Press [.4] Enroll In All of the above");
choice = input.next();
switch (choice) {
case "1":
maths.addStudent(s1);
break;
case "2":
excel.addStudent(s1);
break;
case "3":
java.addStudent(s1);
break;
case "4":
maths.addStudent(s1);
excel.addStudent(s1);
java.addStudent(s1);
}
break;
case "2":
System.out.println("Press [.1] to View Maths Class List");
System.out.println("Press [.2] to View Excel Class List");
System.out.println("Press [.3] to View Java Class List");
choice = input.next();
switch (choice) {
case "1":
maths.printSubjectDetails();
maths.printClass();
break;
case "2":
excel.printSubjectDetails();
excel.printClass();
break;
case "3":
java.printSubjectDetails();
java.printClass();
break;
case "4":
maths.printSubjectDetails();
maths.printClass();
excel.printSubjectDetails();
excel.printClass();
java.printSubjectDetails();
java.printClass();
break;
}
break;
case "3":
System.out.println("Which student would you like to remove ");
break;
}
}
} while (!"5".equals(choice));
}
public static class Subject {
/*(Public Variables)*/
public String subjectName;
public String subjectTutor;
public ArrayList<Student> studentList = new ArrayList<Student>();
public Subject(String sName, String tutor) {
subjectName = sName;
subjectTutor = tutor;
}
public void printSubjectDetails()//Print Subject Details Method//Print the subject details listed below.
{
System.out.println("Subjects are " + subjectName + " and tutor is " + subjectTutor);
}
public void addStudent(Student student) {
studentList.add(student);
System.out.println("Now have " + studentList.size() + " students for " + subjectName);
}
public void printClass()//Print method that prints out the selected class when called in the main method.
{
System.out.println(subjectName + " Class List");
for (Student i : studentList) {
i.printStudent();
}
}
}
public static class Student {
//variables
private String studentFName;
private String studentLName;
int studentGrade;
public Student() {
Scanner input = new Scanner(System.in);
//Operations for student class.
System.out.println("Please enter First Name.");
studentFName = input.next();
System.out.println("Please enter student Surname.");
studentLName = input.next();
}
//Print operations for student class.
public void printStudent() {
System.out.println("Students First Name is:" + studentFName);
System.out.println("Students Surname is:" + studentLName);
}
}
}