Java循环以及如何解决它

时间:2022-11-07 19:02:45

I think my loop is wrong, how can I fix it so I get all the names to be printed out?

我认为我的循环是错误的,我该如何修复它以便我将所有名称打印出来?

// Course class
public class Course {

    // Data fields
    private String courseName;
    private String[] students = new String[100];
    private int numberOfStudents;

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

    // Methods 
    public String getCourseName() {
        return courseName;
    }

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

    public void dropStudent(String student) {

    }

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

    public int getNumberOfStudents() {
        return numberOfStudents;
    }
}

Test the class

测试课程

public class TestCourse {

    public static void main(String[] args) {
        Course course1 = new Course("Data Structures");
        Course course2 = new Course("Database Systems");

        course1.addStudent("Ben Johnson");
        course1.addStudent("Mary March");
        course1.addStudent("Nora Bently");

        course2.addStudent("John Tailor");
        course2.addStudent("Sara Gardner");

        System.out.println("Number of students in Data Structure Course is: " + 
                              course1.getNumberOfStudents());

        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++);
            System.out.print(students[i] + ", ");

            System.out.println();
            System.out.print("Number of students in Database Systems is: " + 
                               course2.getNumberOfStudents());


    }       
}

So if you run this program there's an error with the TestCourse and I need to fix the loop but it looks right to me. Apparently it's not! Can anybody fix this?

因此,如果您运行此程序,TestCourse出现错误,我需要修复循环,但它对我来说是正确的。显然它不是!有人能解决这个问题吗?

5 个解决方案

#1


0  

Your for loop in your test class is missing braces.

您的测试类中的for循环缺少大括号。

for (int i = 0; i < course1.getNumberOfStudents(); i++);
    System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in Database Systems is: " + 
                       course2.getNumberOfStudents());

needs to be

需要是

for (int i = 0; i < course1.getNumberOfStudents(); i++) {
    System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in Database Systems is: " + 
                       course2.getNumberOfStudents());
}

White space in java is ignored. For more reading, read this.

java中的空格被忽略。如需更多阅读,请阅读此内容。

#2


0  

for (int i = 0; i < course1.getNumberOfStudents(); i++);

just remove ; here (if use ; this means that the loop does nothing)

只是删除;这里(如果使用;这意味着循环什么都不做)

 for (int i = 0; i < course1.getNumberOfStudents(); i++)

#3


0  

public class TestCourse {

    public static void main(String[] args) {
        Course course1 = new Course("Data Structures");
        Course course2 = new Course("Database Systems");

        course1.addStudent("Ben Johnson");
        course1.addStudent("Mary March");
        course1.addStudent("Nora Bently");

        course2.addStudent("John Tailor");
        course2.addStudent("Sara Gardner");

        System.out.println("Number of students in Data Structure Course is: " + 
                              course1.getNumberOfStudents());

        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++) {
            System.out.print(students[i] + ", ");

            System.out.println();
            System.out.print("Number of students in Database Systems is: " + 
                               course2.getNumberOfStudents());

        }
    }       
}

I think now it is fine.

我想现在好了。

#4


0  

Change to the following for loop to this (You have a typo error):

更改为以下for循环到此(您有拼写错误):

for (int i = 0; i < course1.getNumberOfStudents(); i++)
{
    System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in Database Systems is: " + course2.getNumberOfStudents());
}

You had a semicolon at the end of the for loop, and you had to replace it with a bracket to indicate the loop block.

你在for循环结束时有一个分号,你必须用括号替换它来表示循环块。

#5


-1  

Update your code - you are missing braces around the if loop:

更新你的代码 - 你缺少if循环的大括号:

for (int i = 0; i < course1.getNumberOfStudents(); i++) {
            System.out.print(students[i] + ", ");
}

Indentation in Java doesn't provide the looping; braces do. (you had a ; which meant 'do nothing' for the loop body.)

Java中的缩进不提供循环;大括号。 (你有一个;这意味着循环体'什么都不做'。)

#1


0  

Your for loop in your test class is missing braces.

您的测试类中的for循环缺少大括号。

for (int i = 0; i < course1.getNumberOfStudents(); i++);
    System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in Database Systems is: " + 
                       course2.getNumberOfStudents());

needs to be

需要是

for (int i = 0; i < course1.getNumberOfStudents(); i++) {
    System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in Database Systems is: " + 
                       course2.getNumberOfStudents());
}

White space in java is ignored. For more reading, read this.

java中的空格被忽略。如需更多阅读,请阅读此内容。

#2


0  

for (int i = 0; i < course1.getNumberOfStudents(); i++);

just remove ; here (if use ; this means that the loop does nothing)

只是删除;这里(如果使用;这意味着循环什么都不做)

 for (int i = 0; i < course1.getNumberOfStudents(); i++)

#3


0  

public class TestCourse {

    public static void main(String[] args) {
        Course course1 = new Course("Data Structures");
        Course course2 = new Course("Database Systems");

        course1.addStudent("Ben Johnson");
        course1.addStudent("Mary March");
        course1.addStudent("Nora Bently");

        course2.addStudent("John Tailor");
        course2.addStudent("Sara Gardner");

        System.out.println("Number of students in Data Structure Course is: " + 
                              course1.getNumberOfStudents());

        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++) {
            System.out.print(students[i] + ", ");

            System.out.println();
            System.out.print("Number of students in Database Systems is: " + 
                               course2.getNumberOfStudents());

        }
    }       
}

I think now it is fine.

我想现在好了。

#4


0  

Change to the following for loop to this (You have a typo error):

更改为以下for循环到此(您有拼写错误):

for (int i = 0; i < course1.getNumberOfStudents(); i++)
{
    System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in Database Systems is: " + course2.getNumberOfStudents());
}

You had a semicolon at the end of the for loop, and you had to replace it with a bracket to indicate the loop block.

你在for循环结束时有一个分号,你必须用括号替换它来表示循环块。

#5


-1  

Update your code - you are missing braces around the if loop:

更新你的代码 - 你缺少if循环的大括号:

for (int i = 0; i < course1.getNumberOfStudents(); i++) {
            System.out.print(students[i] + ", ");
}

Indentation in Java doesn't provide the looping; braces do. (you had a ; which meant 'do nothing' for the loop body.)

Java中的缩进不提供循环;大括号。 (你有一个;这意味着循环体'什么都不做'。)