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,在收集名字和姓氏之后,我将数组按字母顺序排列,然后查询每个人有多少学生。使用 settersetnumStudents
我尝试更改numStudents
存储在数组中。使用我的代码,我得到这个错误:
non-static method setnumStudents(int) cannot be referenced from a static context
otherClass.setnumStudents(val);
^
感谢您对此问题的任何帮助