添加和删​​除学生的逻辑错误[Java] [英] Logic Error in Adding and Dropping Students [Java]

查看:144
本文介绍了添加和删​​除学生的逻辑错误[Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个程序,我必须添加和删除两个课程,使用两个类(面向对象编程)的学生。到目前为止,这是我的代码:

  public class Course {
private String courseName;
private String [] students = new String [100];
private int numberOfStudents;

public Course(String courseName){
this.courseName = courseName;
}

public void addStudent(String student){
students [numberOfStudents] = student;
numberOfStudents ++;
}

public String [] getStudents(){
return students;
}

public int getNumberOfStudents(){
return numberOfStudents;
}

public String getCourseName(){
return courseName;
}

public void dropStudent(String student){
//为for循环设置一个变量
int indexOfStudentToDrop = -1;

//设置一个for循环,有两个if语句会相应地丢弃学生
for(int i = 0; i // if这个if语句是true,drop student
if(students [i] .equalsIgnoreCase(student)){

indexOfStudentToDrop = i;
}
//如果这个if语句为true,不要删除学生并增加数组
if(indexOfStudentToDrop!= -1){
for(i = indexOfStudentToDrop; i students [i] = students [i + 1];
} // end if found

//通过1减少学生数量
numberOfStudents--;
} // end if equal


}

public void clear(){

//迭代学生数组,全部赋值为null
for(int i = 0; i students [i] = null;
}

//将学生数分配为零
numberOfStudents = 0;

}

public void increaseArray(int amount){

students = new String [students.length + amount];

}
}

  public class TestCourse {
public static void main(String [] args){

//创建两个课程
Course course1 = new Course(Data Structures);
Course course2 = new Course(Database Systems);

//介绍程序
System.out.println(Creating Two Courses);
System.out.println(将6名学生添加到课程1);
System.out.println(将3个学生添加到课程2);

//将六个学生添加到course1
course1.addStudent(\\\
1:Tom Servo);
course1.addStudent(\\\
2:Joel Robinson);
course1.addStudent(\\\
3:Mike Nelson);
course1.addStudent(\\\
4:Pearl Forrester);
course1.addStudent(\\\
5:TV's Frank);
course1.addStudent(\\\
6:Zap Rowsdower);



//将三个学生添加到course2
course2.addStudent(\\\
1:Tom Servo);
course2.addStudent(\\\
2:Crow T. Robot);
course2.addStudent(\\\
3:Zap Rowsdower);

//打印每个课程中的学生数量
System.out.println(course1中的学生数:+ course1.getNumberOfStudents()+学生是:) ;
String [] students = course1.getStudents();
for(int i = 0; i System.out.print(students [i] +);

System.out.println();
System.out.print(\\\
Number of students in course2:+ course2.getNumberOfStudents()+Students are:);
String [] students2 = course2.getStudents();
for(int i = 0; i System.out.print(students2 [i] +);

//告诉用户计划从每个课程中删除多少学生
System.out.println(\ n \\\
dropping 2 students from course 1);
System.out.println(\\\
dropping 1 student from course 2);

//丢下一些学生。
course1.dropStudent(Tom Servo);
course1.dropStudent(Joel Robinson);
System.out.println(\\\
课程1中的学生数:+学生是:+\\\
+ course1);

course2.dropStudent(Crow T. Robot);
System.out.println(\\\
课程2中的学生数:+学生是:+\\\
+ course2);

//清除course2,但保持course1为当前状态
System.out.println(\\\
\\\
clearing course 2 course 2);
course2.clear();

System.out.println(\\\
课程1中的学生数量:+学生为:+\\\
+ course1);
System.out.println(\\\
课程2中的学生数:+学生是:+\\\
+ course2);
}

}

 创建两个课程
将6个学生添加到课程1
将3个学生添加到课程2

课程1:6学生人数:
1:Tom Servo
2:Joel Robinson
3:Mike Nelson
4:Pearl Forrester
5:电视的Frank
6:Zap Rowsdower

课程2:3学生人数:
1:Tom Servo
2:Crow T 。机器人
3:Zap Rowsdower

从课程中减去2名学生1
从课程中减掉1名学生2

课程1:4的学生人数学生是:
1:Mike Nelson
2:珍珠Forrester
3:电视的Frank
4:Zap Rowsdower

课程2中的学生人数: 2学生是:
1:Tom Servo
2:Zap Rowsdower

清除课程2课程2

课程1:4学生人数是:
1:Mike Nelson
2:Pearl Forrester
3:电视的Frank
4:Zap Rowsdower

课程2中的学生数量:0学生是:

但这是我运行代码时会发生什么:



创建两个课程
将6个学生添加到课程1
将3个学生添加到课程2
课程中的学生数量1: 6学生是:

1:Tom Servo
2:Joel Robinson
3:Mike Nelson
4:Pearl Forrester
5:电视的Frank
6:Zap Rowsdower

课程中的学生数量2:3学生是:
1:Tom Servo
2:Crow T. Robot
3:Zap Rowsdower

从课程中减掉2个学生1

从课程中减少1个学生2

课程1中的学生数量:学生是:
课程@ 6d06d69c

课程2中的学生人数:学生是:
课程@ 7852e922


清除课程2课程2

课程1中的学生数量:学生是:
课程@ 6d06d69c

课程2中的学生数量:学生是:
课程@ 7852e922

如您所见,在初始列表之后,发生了两件事:



1)程序已停止计数
2)名称已停止列出

解决方案

首先,你真的应该使用列表,因为你在删除和添加学生时更改数组的大小。无论如何...



因此,您在输出底部看到的是指向课程对象的指针。换句话说,你看到的信息存储在对象的方向,而不是信息本身。当你尝试打印一个没有 toString()方法的对象时(例如当你写 System.out.println(。 .. + course1)。此方法允许您基本设置打印对象时显示的内容,并且覆盖对象指针的默认值。



在这种情况下,您要显示的信息是学生的姓名,有多少学生,以及可能的课程名称。 toString()在Course类中)将如下所示:

  public String toString(){

String toReturn =;

toReturn + =学生人数+ courseName +:
+ numberOfStudents +学生是:\\\
;

for(int i = 0; i
toReturn + =(i + 1)+:+ students [i] + \\ n;

}

return toReturn;

}

如果你将这个方法添加到类中,那么你甚至不需要所有打印指针之前打印信息的代码。您可以直接打印课程对象。


For this program, I have to add and drop students from two courses, which uses two classes (object-oriented programming). So far, this is my code:

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;

public Course(String courseName) {
    this.courseName = courseName;
}

public void addStudent(String student) {
    students[numberOfStudents] = student;
    numberOfStudents++;
}

public String[] getStudents() {
    return students;
}

public int getNumberOfStudents() {
    return numberOfStudents;
}

public String getCourseName() {
    return courseName;
}

public void dropStudent(String student) {
    // set up a variable for the for loop
    int indexOfStudentToDrop = -1;

    // set up a for loop, with two if statements that drop students accordingly
    for (int i = 0; i < numberOfStudents; i++) {
        // if this if statement is true, drop student
        if(students[i].equalsIgnoreCase(student)) {

            indexOfStudentToDrop = i;
        }
        // if this if statement is true, DON'T drop student and increase the array
        if (indexOfStudentToDrop != -1) {
            for (i = indexOfStudentToDrop; i < numberOfStudents; i++)
                students[i] = students[i+1];
        } // end if found

        // decrement number of students by 1
        numberOfStudents--;
    } // end if equal


}

public void clear() {

    // iterating on the students array, assign all to null
    for (int i = 0; i < numberOfStudents; i++) {
        students[i] = null;
    }

    // assign number of students to zero
    numberOfStudents = 0;

}

public void increaseArray(int amount) {

    students = new String[students.length + amount];

}
}

and

public class TestCourse {
    public static void main(String[] args) {

        // create two courses
        Course course1 = new Course("Data Structures");
        Course course2 = new Course("Database Systems");

        // introduce the program
        System.out.println("Creating Two Courses");
        System.out.println("Adding 6 students to course 1");
        System.out.println("Adding 3 students to course 2");

        // add six students to course1
        course1.addStudent("\n1: Tom Servo");
        course1.addStudent("\n2: Joel Robinson");
        course1.addStudent("\n3: Mike Nelson");
        course1.addStudent("\n4: Pearl Forrester");
        course1.addStudent("\n5: TV's Frank");
        course1.addStudent("\n6: Zap Rowsdower");



        // add three students to course2
        course2.addStudent("\n1: Tom Servo");
        course2.addStudent("\n2: Crow T. Robot");
        course2.addStudent("\n3: Zap Rowsdower");

        // print out the number of students in each course
        System.out.println("Number of students in course1: " + course1.getNumberOfStudents() + " Students are: ");
        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++)
            System.out.print(students[i] + " ");

        System.out.println();
        System.out.print("\nNumber of students in course2: " + course2.getNumberOfStudents() + " Students are: ");
        String[] students2 = course2.getStudents();
        for (int i = 0; i < course2.getNumberOfStudents(); i++)
            System.out.print(students2[i] + " ");

        // tell user how many students you plan to drop from each course
        System.out.println ("\n\ndropping 2 students from course 1");
        System.out.println ("\ndropping 1 student from course 2");

        // drop some students.
                course1.dropStudent("Tom Servo");
                course1.dropStudent("Joel Robinson");
                System.out.println ("\nNumber of students in Course 1: " + " Students are: " + "\n" + course1);

                course2.dropStudent("Crow T. Robot");
                System.out.println("\nNumber of students in Course 2: " + " Students are: " + "\n" + course2);

        // clear course2, but keep course1 as it currently stands
                System.out.println("\n\nclearing course 2 course 2");
                course2.clear();

                System.out.println("\nNumber of students in Course 1: " + " Students are: " + "\n" + course1);
                System.out.println("\nNumber of students in Course 2: " + " Students are: " + "\n" + course2);
        }

}

This is how it's supposed to look:

Creating Two Courses
Adding 6 students to course 1
Adding 3 students to course 2

Number of students in Course 1: 6 Students are: 
1: Tom Servo
2: Joel Robinson
3: Mike Nelson
4: Pearl Forrester
5: TV's Frank
6: Zap Rowsdower

Number of students in Course 2: 3 Students are: 
1: Tom Servo
2: Crow T. Robot
3: Zap Rowsdower

dropping 2 students from course 1
dropping 1 student from course 2

Number of students in Course 1: 4 Students are:
1: Mike Nelson
2: Pearl Forrester
3: TV's Frank
4: Zap Rowsdower

Number of students in Course 2: 2 Students are:
1: Tom Servo
2: Zap Rowsdower

clearing course 2 course 2

Number of students in Course 1: 4 Students are:
1: Mike Nelson
2: Pearl Forrester
3: TV's Frank
4: Zap Rowsdower

Number of students in Course 2: 0 Students are:

But this is what happens when I run the code:

Creating Two Courses
Adding 6 students to course 1
Adding 3 students to course 2
Number of students in course1: 6 Students are: 

1: Tom Servo 
2: Joel Robinson 
3: Mike Nelson 
4: Pearl Forrester 
5: TV's Frank 
6: Zap Rowsdower 

Number of students in course2: 3 Students are: 
1: Tom Servo 
2: Crow T. Robot 
3: Zap Rowsdower 

dropping 2 students from course 1

dropping 1 student from course 2

Number of students in Course 1:  Students are: 
Course@6d06d69c

Number of students in Course 2:  Students are: 
Course@7852e922


clearing course 2 course 2

Number of students in Course 1:  Students are: 
Course@6d06d69c

Number of students in Course 2:  Students are: 
Course@7852e922

As you can see, after the initial listings, two things have happened:

1) The program has stopped counting 2) The names have stopped being listed

解决方案

First things first, you really should be using a list, as you are changing the size of the array when dropping and adding students. Anyways...

So what you are seeing at the bottom of the output you got are the pointers to the Course objects. In other words, you are seeing the directions to the information stored in the objects, not the info itself. This happens when you try to print an object that does not have a toString() method (as in when you write System.out.println(... + course1). This method lets you basically set what is displayed when printing an object, and sort of overrides the default of the object pointer.

In this case, the info you want to display are the students' names, how many students there are, and possibly the course name. Your toString() method (in the Course class) would then look something like this:

public String toString(){

    String toReturn = "";

    toReturn += "Number of students in " + courseName + ": "
                    + numberOfStudents + " Students are:\n";

    for(int i = 0; i < number of students - 1; i++){

        toReturn += (i + 1) + ": " + students[i] + "\n";

    }

    return toReturn;

}

If you add this method to the class, then you don't even need all the code printing the info that worked before you printed the pointers. You can just print the Course object as is.

这篇关于添加和删​​除学生的逻辑错误[Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆