public class Thing
{
public static void main (String[] args)
{
//I set my veriables and prompt user to enter number of names
//Prompt user for First and Last name and Store names in an array of classes
Arrays.sort(listOfName, new Comparator<otherClass>()//Sort names according to last name
//code here for that (which i have but not the issue)
for (int i = 0; i<numName; i++)
{
ans = JOptionPane.showInputDialog(null,"How Many Students does: \n "+ listOfName[i]+ " Have");
int val = Integer.parseInt(ans);
otherClass.setnumStudents(val);// in my Separate class I have a setnumStudents
}
for(int i = 0; i<numName; i++)
{
System.out.println(listOfName[i]);
}
}
プログラムの前半で、別のクラスに従って格納するように配列を設定しotherClass
、int numStudents
最初は 0 に設定します。firstName と lastName を収集した後、配列をアルファベット順に並べて、各人の生徒数を調べます。セッターを使用して、配列に格納されているsetnumStudents
ものを変更しようとします。numStudents
私のコードをそのまま使用すると、次のエラーが発生します。
non-static method setnumStudents(int) cannot be referenced from a static context
otherClass.setnumStudents(val);
^
この問題について助けてくれてありがとう