import java.io.IOException;
import java.util.Scanner;
public class ReptileEnclosure { /**
 * @param args
 */
Reptile myreptile[]=new Reptile[20];
int reptilenum=0;
int reptilefor;
public static void main(String[] args) throws IOException {
int mychoice;
while(true){
System.out.println("1. addReptile, 2. deleteReptile, 3. displayReptiles, 4.Quit:");
mychoice=(int)System.in.read();
switch(mychoice){
case 1:addReptile();break;
case 2:deleteReptile();break;
case 3:displayReptiles();break;
case 4:break;
default:break;
}
} }
public void addReptile()
{
double testdosage;
String testfoodtype;
int testyear;
int testmonth;
int testday;
String testrtype;
int testrid;
boolean uniquerid;
int testrage;
Scanner readera=new Scanner(System.in);
testdosage=readera.nextDouble();
Scanner readerb=new Scanner(System.in);
testfoodtype=readerb.next();
Scanner readerc=new Scanner(System.in);
testrtype=readerc.next();
Scanner readerd=new Scanner(System.in);
testyear=readerd.nextInt();
Scanner readere=new Scanner(System.in);
testmonth=readere.nextInt();
Scanner readerf=new Scanner(System.in);
testday=readerf.nextInt();
Scanner readerg=new Scanner(System.in);
testrid=readerg.nextInt();
uniquerid=uniqueid(testrid);
Scanner readerh=new Scanner(System.in);
testrage=readerh.nextInt();
food testfood=new food(testdosage,testfoodtype);
Date testdate=new Date(testyear,testmonth,testday);
if(testfood.valdosage()&&testfood.valfoodtype()&&testdate.valdate()&&uniquerid){
myreptile[reptilenum]=new Reptile(testrid,testrtype,testrage,testdate,testfood);
reptilenum++;
}
else{System.out.println("There is something wrong with your input!");}
}
public void deleteReptile()
{
reptilenum--;
}
public void displayReptiles()
{
for(reptilefor=0;reptilefor<reptilenum;reptilefor++){
System.out.println(myreptile[reptilefor].Rid);
}
}
public boolean uniqueid(int inputid)
{
for(reptilefor=0;reptilefor<reptilenum;reptilefor++){
if(inputid==myreptile[reptilefor].Rid){return false;}
}
return true;
}

}
eclipse里这三行显示有错,为什么呢?
case 1:addReptile();break;
case 2:deleteReptile();break;
case 3:displayReptiles();break;
哪位来给说一下

解决方案 »

  1.   

    你不能在main()方法中直接调用addReptile();因为你在main()方法中只能调用静态方法
    你将public void addReptile() 改为public static void addReptile() 就可以了.
      

  2.   

    那就是说main方法中不能调用method?
      

  3.   

    将public void addReptile() 改为public static void addReptile() 
    将会出现新的错误
      

  4.   

    新的错误是因为你改成静态的以后,这个add方法里面大概 有引用了非静态变量,所以还是这个问题,建议将所有的成员变量全部加上static,那样就没事了,但是这样不符合规范!
      

  5.   

    你可以把方法写在构造函数里
    然后main函数里new一下,再调用就不用改成static了
      

  6.   

    The variables you are using in static methods need to be changed to static...check them out!
      

  7.   

    你把它分成2个文件来写
    main方法里调另一个文件的方法
      

  8.   

    一种方法,把这个类里面的成员变量,和成员方法全部加上static关键字,
    不过这样做不是面向对象编程的风格:
    static Reptile myreptile[] = new Reptile[20];
    static int reptilenum = 0;
    static int reptilefor;public static void addReptile()
    public static void deleteReptile()
    public void displayReptiles()
    public static boolean uniqueid(int inputid)还有一个办法就是在main方法里面ReptileEnclosure re= newReptileEnclosure();
    这样新建一个ReptileEnclosure对象之后,在main方法里面调用方法的时候都用 re.方法名  的形式调用。
      

  9.   

    调是能调但是要有类对象~否则就必须是static的方法了
      

  10.   

    全部加上static关键字
    我知道这样做可以,但是不想这么做,就像10楼说的这样做不是面向对象编程的风格
    我去试一下他的第二个方法,
    有没有更好的方法
      

  11.   

    实例化一下
    ReptileEnclosure re = new ReptileEnclosure();
    switch(mychoice){ 
    case 1:re.addReptile();break; 
    case 2:re.deleteReptile();break; 
    case 3:re.displayReptiles();break; 
    case 4:break; 
    default:break; 

      

  12.   

    还是自己学艺不精,
    现在明白是怎么回事了
    原来是调用了class的method
    谢谢大家了