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;
哪位来给说一下
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;
哪位来给说一下
解决方案 »
- 优化Java代码,提高性能
- 关于binarySearch的一个问题
- 救命啊,没有鼠标监听的item扩展类,怎么获取
- hibernate和jdbc的批量问题一系列问题
- 编写一个Java程序,模仿Notepad记事本的功能,在输入框中输入文字,单击“保存”按钮时自动将文字保存到testout.txt文件中
- 如何在内部类中调用外面的变量,下面的错误清高手解释一下?
- 关于xml的简单问题????????????
- 来了杭州半个月 居然找不到工作
- 如何将这样的字符串,分解为数组?
- package & import 的一个奇怪问题
- 为什么我这个程序运行后在文本文件里没有写入5个随机数呢
- 关于thinking in java 中的一个疑问
你将public void addReptile() 改为public static void addReptile() 就可以了.
将会出现新的错误
然后main函数里new一下,再调用就不用改成static了
main方法里调另一个文件的方法
不过这样做不是面向对象编程的风格:
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.方法名 的形式调用。
我知道这样做可以,但是不想这么做,就像10楼说的这样做不是面向对象编程的风格
我去试一下他的第二个方法,
有没有更好的方法
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;
}
现在明白是怎么回事了
原来是调用了class的method
谢谢大家了