我的psvm类没有运行其他类和方法

时间:2015-05-21 09:04:47

标签: java class methods

我的程序没有运行。它必须是我psvm

中的类调用

我已经插入了我的主类,我想要使用以下方法运行该类。它运行程序,但类:课程及其方法未激活。

public class main {

    public static void main(String[] args) {

    Course task1 = new Course();
    task1.Assignment1();

    }}

另一个班级

public class Course {

    CourseGrades[] course;

    public void Assignment1() {

        Scanner sc = new Scanner(System.in);


            System.out.println("Welcome to students database");
            System.out.println("Enter a to enter a student");
            char choice = sc.next().charAt(0);

            if (choice > 'z' || choice > 'a') {
                System.out.println("Invalid choice");

            } else {

                switch (choice) {

                    case 'a':
                        numbers();
                        break;
                    case 'b':
                        entering();
                        break;
                    case 'c':
                        printCourse();
                        break;

                }
            }
        }


    public void numbers() {

        System.out.println("Enter how many courses taken ");
        Scanner numbers = new Scanner(System.in);
        int courses = numbers.nextInt();
        CourseGrades[] course = new CourseGrades[courses];

    }

    public void entering() {

        for (int i = 0; i < course.length; i++) {
            System.out.println("Enter  coursename");
            Scanner sc = new Scanner(System.in);
            sc = new Scanner(System.in);
            String courseName = sc.nextLine();
            course[i] = new CourseGrades();
            course[i].setName(courseName);
            System.out.println("Enter grade");
            Scanner grade = new Scanner(System.in);
            course[i].setGrade(i);

        }
    }

    public void printCourse() {

        System.out.println("Printing out courses and grades");

        for (int i = 0; i < course.length; i++) {

            System.out.println(" name 2" + course[i].getName() + " grade" + course[i].getGrade());

        }

    }
}

吸气剂和制定者

public class CourseGrades {

private String name;
private int grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    } 
}

1 个答案:

答案 0 :(得分:1)

如果代码中有多个类,则应始终使用具有main()方法的类(在本例中为Main.java)进行保存,并且该类只应包含公共访问说明符。我在IDE中运行相同的代码。我在MY IDE中成功运行您的代码:

参见代码:

         public class Main {


    public static void main(String[] args) {

    Course task1 = new Course();
    task1.Assignment1();

    }

 }

// The other class

class Course {

    CourseGrades[] course;

    public void Assignment1() {

        Scanner sc = new Scanner(System.in);


            System.out.println("Welcome to students database");
            System.out.println("Enter a to enter a student");
            char choice = sc.next().charAt(0);

            if (choice > 'z' || choice > 'a') {
                System.out.println("Invalid choice");

            } else {

                switch (choice) {

                    case 'a':
                        numbers();
                        break;
                    case 'b':
                        entering();
                        break;
                    case 'c':
                        printCourse();
                        break;

                }
            }
        }


    public void numbers() {

        System.out.println("Enter how many courses taken ");
        Scanner numbers = new Scanner(System.in);
        int courses = numbers.nextInt();
        CourseGrades[] course = new CourseGrades[courses];

    }

    public void entering() {

        for (int i = 0; i < course.length; i++) {
            System.out.println("Enter  coursename");
            Scanner sc = new Scanner(System.in);
            sc = new Scanner(System.in);
            String courseName = sc.nextLine();
            course[i] = new CourseGrades();
            course[i].setName(courseName);
            System.out.println("Enter grade");
            Scanner grade = new Scanner(System.in);
            course[i].setGrade(i);

        }
    }

    public void printCourse() {

        System.out.println("Printing out courses and grades");

        for (int i = 0; i < course.length; i++) {

            System.out.println(" name 2" + course[i].getName() + " grade" + course[i].getGrade());

        }

    }
}

// Getters and setters
class CourseGrades {

private String name;
private int grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }


} 

结果:

       Welcome to students database
        Enter a to enter a student
        a
        Enter how many courses taken 
        3