我的班级看起来像这样:
public class Month
{
private int numOfMonth;
private int monthNum;
public int monthNum()
{
return monthNum = 1;
}
public void setMonthNum(int monthNum){
switch (monthNum)
{
case 1: System.out.println("January"); break;
case 2: System.out.println("February");break;
case 3: System.out.println("March");break;
case 4: System.out.println("April");break;
case 5: System.out.println("May");break;
case 6: System.out.println("June");break;
case 7: System.out.println("July");break;
case 8: System.out.println("August");break;
case 9: System.out.println("September");break;
case 10: System.out.println("October");break;
case 11: System.out.println("November");break;
case 12: System.out.println("December");break;
}
}
public String getName()
{
return "" + monthNum;
}
}
我的驱动如下:
import java.util.Scanner;
public class monthDriver
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter month number: ");
int monthNum = in.nextInt();
System.out.println("Month number " + monthNum + " is the month of " + Month.getName());
}
}
我得到编译时错误:
"monthDriver.java:12: error: non-static method getName() cannot be referenced from a static context
System.out.println("Month number " + monthNum + " is the month of " + Month.getName());1 error"
请记住,我是一名学生,学术诚信对我很重要,为什么我会收到这样的错误?另外,以后有什么建议可以提高我的编码效率吗?感谢您所有的时间和精力。非常感谢。