0

我的执行显示此错误:

java.lang.NoSuchMethodError:主要

我需要让学生详细信息作为输出。该程序应访问上述所有方法。

 import java.io.*;
    import java.io.IOException;

    public class Student {
        String name;
        int age;
        String clas;
        int rank;
        int ID;

        public Student(String name){
        this.name = name;
        }

        public void studAge(int studAge)
        {
            age=studAge;
        }
        public void studClas(String studClas)
        {
            clas=studClas;
        }
        public void studRank(int studRank)
        {
            rank=studRank;
        }
        public void studID(int studID)
        {
            ID=studID;
        }
        public void displayStud()
        {
            System.out.println("Student Age is:"+age);
            System.out.println("Student Class is:"+clas);
            System.out.println("Student Rank is:"+rank);
            System.out.println("Student ID is:"+ID);
        }
    }
    class StudRecord{
       public static void main(String [] args)throws IOException
       {            
        Student studOne=new Student("Faraz");
        Student studTwo=new Student("Musheer");
        Student studThree=new Student("Imdad");
        Student studFour=new Student("Shahid");

        // Invoking methods for each object created         
        studOne.studAge(23);
        studOne.studClas("11th");
        studOne.studID(130018);
        studOne.studRank(16);

        studFour.studAge(21);
        studFour.studClas("11th");
        studFour.studID(130035);
        studFour.studRank(33);

        studTwo.studAge(26);
        studTwo.studClas("10th");
        studTwo.studID(130021);
        studTwo.studRank(2);

        studThree.studAge(24);
        studThree.studClas("11th");
        studThree.studID(130032);
        studThree.studRank(32);

        System.out.println();           
       }
   }
4

1 回答 1

0

In Java only one class in a file may be marked public. In this case the class Student is marked public and therefore the NoSuchMethodException is thrown because the main method is not on the Student class but on the StudRecord class.

Therefore change the StudRecord class to public and the Student class to default access (no access modifier).

于 2013-02-08T12:37:30.310 回答