我无法访问类中的方法

大卫

这是我第一次尝试用Java创建对象数组。我的代码混乱不堪,充满了愚蠢的初学者错误。

package javaapplication62;

import java.util.Scanner;

public class JavaApplication62 {

    public static void main(String[] args) {

        while (true) {

            Scanner in = new Scanner(System.in);

            System.out.println("1.Enroll Student");
            System.out.println("2.Offer Course");

            String name = in.next();
            int choice = in.nextInt();

            if (choice == 1) {

                student_class[] studentList = new student_class[500];

                studentList[0] = new student_class();

                while (true) {

                    System.out.print(" Enter name of the student: ");
                    studentList[0].setName(in.next());

                    System.out.println("");
                    System.out.print(" The student is given the unique ID =  " + studentList[0].getUniqueStudentID() + " and the number of students =  " + studentList[0].getNumberOfStudents());

                    System.out.print("> and the number of students :  ");
                    studentList[0].setnumberOfStudents(in.nextInt);

                    System.out.println("");
                    System.out.print(" Enter the e-mail address of the student: ");
                    studentList[0].setemail(in.next);

                    System.out.print("Enter the semester number of the student:  ");
                    studentList[0].setsemesterNumber(in.nextInt);

                    System.out.print(" Add another student (Y/N): ");
                    char add = in.next().charAt(0);
                    if (add == 'Y' || add == 'y') {

                    }

                    if (add == 'N' || add == 'n') {
                        break;
                    }

                }

            } else if (choice == 2) {

                while (true) {

                    Course[] courseOfferedList;
                    courseOfferedList = new Course[500];
                    courseOfferedList[0] = new Course();

                    System.out.println("Enter the course code : ");
                    courseOfferedList[0].setCourseCode(in.next());

                    System.out.println(" Enter the course title: ");
                    courseOfferedList[0].setCourseTitl(in.next());

                    System.out.println(" Enter the credit hours of the course: ");
                    courseOfferedList[0].setCreditHours(in.nextInt());

                    System.out.print(" Add another course (Y/N): ");
                    char add2 = in.next().charAt(0);
                    if (add2 == 'Y' || add2 == 'y') {
                    }

                    if (add2 == 'N' || add2 == 'n') {
                        break;
                    }

                }

            }

        }
    }

}

学生班。

    public class student_class {
    private String name;
    private int uniqueStudentID;
    private String email;
    private int mobileNumber;
    private int semesterNumber;
    static int numberOfStudents = 0;

    public student_class() {

        name = null;
        uniqueStudentID = 0;
        email = null;
        mobileNumber = 0;
        semesterNumber = 0;
        numberOfStudents++;

    }

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

    public void setUniqueStudentID(int uniqueStudentID) {
        this.uniqueStudentID = uniqueStudentID;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setMobileNumber(int mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public void setSemesterNumber(int semesterNumber) {
        this.semesterNumber = semesterNumber;
    }

    public void setNumberOfStudents(int numberOfStudents) {
        this.numberOfStudents = numberOfStudents;
    }

    public String getName() {

        return name;
    }

    public int getUniqueStudentID() {

        return uniqueStudentID;

    }

    public String getEmail() {

        return email;

    }

    public int getMobileNumber() {

        return mobileNumber;
    }

    public int getSemesterNumber() {

        return semesterNumber;
    }

    public int getNumberOfStudents() {

        return numberOfStudents;
    }

    void setName() {

    }

    void setuniqueStudentID() {

    }

    void setnumberOfStudents() {

    }

    void setsemesterNumber() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    void setmobileNumber() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    void setemail() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

课程类别:

public class Course {

    private int courseCode;
    private String courseTitl;
    private int creditHours;
    static int numberOfCourses = 0;

    public int getCourseCode() {
        return courseCode;
    }

    public void setCourseCode(int courseCode) {
        this.courseCode = courseCode;
    }

    public String getCourseTitl() {
        return courseTitl;
    }

    public void setCourseTitl(String courseTitl) {
        this.courseTitl = courseTitl;
    }

    public int getCreditHours() {
        return creditHours;
    }

    public void setCreditHours(int creditHours) {
        this.creditHours = creditHours;
    }

    public static int getNumberOfCourses() {
        return numberOfCourses;
    }

    public static void setNumberOfCourses(int numberOfCourses) {
        Course.numberOfCourses = numberOfCourses;   
}}

每当我运行它时,都会显示此错误:

Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at javaapplication62.student_class.setName(student_class.java:11)
    at javaapplication62.JavaApplication62.main(JavaApplication62.java:29)
Java Result: 

这就是我想要的输出

>用于输入,<用于输出

{Display the menu, Choose the menu item}
> 1

> Enter name of the student: john

< The student is given the unique ID = 1 and the number of students = 1

> Enter the e-mail address of the student: [email protected]

> Enter the semester number of the student:  3

> Add another student (Y/N): Y

> Enter name of the student: Ibrahim

< The student is given the unique ID = 2 and the number of students = 2

> Enter the e-mail address of the student:  [email protected]

> Enter the semester number of the student:  3
> Add another student (Y/N): N

{Display the menu, Choose the menu item}
> 2

> Enter the course code: CPCS203

> Enter the course title: Programming 2

> Enter the credit hours of the course: 4
> Add another course (Y/N): Y

> Enter the course code: CPCS204

> Enter the course title: Data Structures

> Enter the credit hours of the course: 3
> Add another course (Y/N): N

{Display the menu, Choose the menu item}
狮子座

您的大多数错误都在JavaApplication62课堂上。我已经纠正了它们,以获得所需的所需输出。您所有的课程仍然有很多问题。我将由您自行纠正。

查看注释以获取更正。

/**
 * Created by Ninan John J P on 6/30/2016.
 */
import java.util.Scanner;

public class JavaApplication62 {

    public static void main(String[] args) {

        while (true) {

            Scanner in = new Scanner(System.in);

            System.out.println("1.Enroll Student");
            System.out.println("2.Offer Course");

            //String name = in.next(); Not required. This will want a text input before proceeding further.
            int choice = in.nextInt();

            if (choice == 1) {

                student_class[] studentList = new student_class[500];

                studentList[0] = new student_class();

                while (true) {

                    System.out.print(" Enter name of the student: ");
                    studentList[0].setName(in.next());

                    System.out.println("");
                    System.out.print(" The student is given the unique ID =  " + studentList[0].getUniqueStudentID() + " and the number of students =  " + studentList[0].getNumberOfStudents());

                   /* System.out.print("> and the number of students :  ");
                    //studentList[0].setnumberOfStudents(in.nextInt);
                    studentList[0].setNumberOfStudents(in.nextInt());
                   */ //This is not required. You have already done this in the previous print statment.

                    System.out.println("");
                    System.out.print(" Enter the e-mail address of the student: ");
                    //studentList[0].setemail(in.next); you are calling the wrong method. Also in.next should be a method.
                    studentList[0].setEmail(in.next());

                    System.out.print("Enter the semester number of the student:  ");
                   // studentList[0].setsemesterNumber(in.nextInt); // same as prev error

                    studentList[0].setSemesterNumber(in.nextInt());

                    System.out.print(" Add another student (Y/N): ");
                    char add = in.next().charAt(0);
                    if (add == 'Y' || add == 'y') {

                    }

                    if (add == 'N' || add == 'n') {
                        break;
                    }

                }

            } else if (choice == 2) {

                while (true) {

                    Course[] courseOfferedList;
                    courseOfferedList = new Course[500];
                    courseOfferedList[0] = new Course();

                    System.out.println("Enter the course code : ");
                    //courseOfferedList[0].setCourseCode(in.next()); // next() returns a string. You need nextInt() here.   

                    courseOfferedList[0].setCourseCode(in.nextInt());

                    System.out.println(" Enter the course title: ");
                    courseOfferedList[0].setCourseTitl(in.next());

                    System.out.println(" Enter the credit hours of the course: ");
                    courseOfferedList[0].setCreditHours(in.nextInt());

                    System.out.print(" Add another course (Y/N): ");
                    char add2 = in.next().charAt(0);

//                    if (add2 == 'Y' || add2 == 'y') { // not needed
//                    }

                    if (add2 == 'N' || add2 == 'n') {
                        break;
                    }

                }

            }

        }
    }

}

另外,我还没有改善您的缩进。您也应该看看。

这将为您提供您在问题中提到的输出。但是我认为您应该完全重构很多代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法访问线程run()中的方法

来自分类Dev

我无法访问Java中的对象

来自分类Dev

无法访问包中的类

来自分类Dev

为什么类的方法无法访问类的某些字段?

来自分类Dev

无法访问父类中定义的方法

来自分类Dev

在AngularJS的类方法中无法访问注入的依赖项

来自分类Dev

无法访问类的属性

来自分类Dev

无法访问类中的属性

来自分类Dev

无法访问可移植类库中的类

来自分类Dev

IllegalAccessError:方法是无法访问类

来自分类Dev

Kotlin-无法访问其他类中的方法

来自分类Dev

无法访问Keyboard类中的功能

来自分类Dev

父类中受保护的内联方法无法访问其他受保护的方法

来自分类Dev

调用特定类实例中的方法的HTML按钮无法访问实例值

来自分类Dev

我无法访问Kotlin中的textView

来自分类Dev

无法访问我在通用方法中用作类型实参的类的基类的属性

来自分类Dev

无法访问组件方法中的$ refs

来自分类Dev

无法访问我班上的方法

来自分类Dev

无法访问线程run()中的方法

来自分类Dev

无法访问父类中定义的方法

来自分类Dev

使控制器中的DbContext类无法访问的最佳方法是什么?

来自分类Dev

无法访问Java中的Class方法

来自分类Dev

无法访问Java中另一个类中方法的返回实例

来自分类Dev

无法访问普通类中的风味类

来自分类Dev

我的公共布尔方法中无法访问的语句

来自分类Dev

无法访问类中受保护的最终同步方法

来自分类Dev

无法访问我的刀片文件中的组件方法

来自分类Dev

我无法访问基类的函数

来自分类Dev

无法访问 scala 类中的 sparkcontext