我学习JAVA来写的第一个小的程序,这个程序主要是识别7种不同的动
import java.awt.*;import java.lang.*;import java.io.*;/*import java.applet.Applet;*/public class AnimalIdentify { /* 数据Boolean 类定义*/ boolean mammal,aves,carnivore,ungulate; boolean hair,feather,claw,sharpTooth,hoof,cloot,longLeg,longNeck; boolean galactopoiesis,fly,layEgg,eatMeat,ruminate,eyeFrontad,notFly,swimming,beGoodAtFly; boolean snuffColor,fuscousFleck,blackStripe,white,blackAndWhite; boolean leopard,tiger,zebra,giraffe,ostrich,penguin,salangane; /* 数据初始化*/ public AnimalIdentify(){ mammal=aves=carnivore=ungulate=true; hair=feather=claw=sharpTooth=hoof=cloot=longLeg=longNeck=false; galactopoiesis=fly=layEgg=eatMeat=ruminate=eyeFrontad=notFly=swimming=beGoodAtFly=false; snuffColor=fuscousFleck=blackStripe=white=blackAndWhite=false; leopard=tiger=zebra=giraffe=ostrich=penguin=salangane=false; } /* 规则定义*/ public boolean rule1 (boolean hair){ if (hair) System.out.println("The animal is a mammal"); return mammal; }
public boolean rule2(boolean galactopoiesis){ if (galactopoiesis) System.out.println("The animal is a mammal"); return mammal; } public boolean rule3(boolean feather){ if(feather) System.out.println("The animal is an aves"); return aves; } public boolean rule4(boolean fly, boolean layEgg){ if (fly&&layEgg) System.out.println("The animal is an aves"); return aves; } public boolean rule5(boolean mammal, boolean eatMeat){ if (mammal&&eatMeat) System.out.println("The animal is a carnivore"); return carnivore; } public boolean rule6(boolean mammal,boolean claw,boolean sharpTooth ,boolean eyeFrontad){ if (mammal&&claw&&sharpTooth&&eyeFrontad) System.out.println("The animal is a carnivore"); return carnivore; } public boolean rule7(boolean mammal,boolean hoof){ if (mammal&&hoof) System.out.println("The animal is a ungulate"); return ungulate; } public boolean rule8(boolean mammal,boolean ruminate){ if(mammal&&ruminate) System.out.println("The animal is ungulate and cloot"); return cloot; } public boolean rule9(boolean carnivore,boolean snuffColor,boolean fuscousFleck){ if(carnivore&&snuffColor&&fuscousFleck) System.out.println("The animal is a leopard"); return leopard; } public boolean rule10(boolean carnivore,boolean snuffColor,boolean blackStripe){ if (carnivore&&snuffColor&&blackStripe) System.out.println("The animal is a tiger"); return tiger; } public boolean rule11(boolean ungulate,boolean longLeg,boolean longNeck,boolean snuffColor,boolean fuscousFleck){ if(ungulate&&longLeg&&longNeck&&snuffColor&&fuscousFleck) System.out.println("The animal is a giraffe"); return giraffe; } public boolean rule12(boolean ungulate,boolean white,boolean blackStripe){ if (ungulate&&white&&blackStripe) System.out.println("The animal is a zebra"); return zebra; } public boolean rule13(boolean aves,boolean notFly,boolean longLeg,boolean longNeck,boolean blackAndWhite){ if (aves&¬Fly&&longLeg&&longNeck&&blackAndWhite) System.out.println("The animal is ostrich"); return ostrich; } public boolean rule14(boolean aves,boolean notFly,boolean swimming,boolean blackAndWhite){ if (aves&¬Fly&&swimming&&blackAndWhite) System.out.println("The animal is a penguin"); return penguin; } public boolean rule15(boolean aves,boolean beGoodAtFly){ if (aves&&beGoodAtFly) System.out.println("The animal is a salangane"); return salangane; } /*规则查询分析*/ public void queryUseRule( ){ System.out.println("please in your complexion about the animal"); System.out.println("the animal doesn't have hair? In(Y/N)"); try { BufferedInputStream st1= new BufferedInputStream (System.in); if(st1.read( )==(int)('Y')){ hair=true; rule1(hair);} }catch(IOException e) { System.out.println("Error reading from user"); }
System.out.println("continue observe"); System.out.println("the animal isn't galactopoiesis? In(Y/N)"); try { BufferedInputStream st1= new BufferedInputStream (System.in); if(st1.read( )==(int)('Y')){ galactopoiesis=true; rule2(galactopoiesis); } }catch(IOException e) { System.out.println("Error reading from user"); }
System.out.println("continue observe"); System.out.println("the animal doesn't have feather? In(Y/N)"); try { BufferedInputStream st1= new BufferedInputStream (System.in); if (st1.read( )==(int)('Y')){ feather=true; rule3(feather); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal doesn't fly? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); boolean t4_1=st1.read()==(int)('Y'); System.out.println("the animal doesn't layegg? In(Y/N)"); boolean t4_2=st2.read()==(int)('Y'); if(t4_1&&t4_2) { fly=true; layEgg=true; rule4(fly,layEgg); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't mammal? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); boolean t5_1=st1.read()==(int)('Y'); System.out.println("the animal doesn't eatMeet? In(Y/N)"); boolean t5_2=st2.read()==(int)('Y'); if (t5_1&&t5_2){ mammal=true; eatMeat=true; rule5(mammal,eatMeat); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't mammal? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); BufferedInputStream st4= new BufferedInputStream (System.in); boolean t6_1=st1.read()==(int)('Y'); System.out.println("the animal doesn't have claw? In(Y/N)"); boolean t6_2=st2.read()==(int)('Y'); System.out.println("the animal doesn't have sharpTooth? In(Y/N)"); boolean t6_3=st3.read()==(int)('Y'); System.out.println("the animal isn't eyeFrontad? In(Y/N)"); boolean t6_4=st4.read()==(int)('Y'); if (t6_1&&t6_2&&t6_3&&t6_4){ mammal=true;claw=true;sharpTooth=true;eyeFrontad=true; rule6(mammal,claw,sharpTooth,eyeFrontad); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't mammal? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); boolean t7_1=st1.read()==(int)('Y'); System.out.println("the animal doesn't have hoof? In(Y/N)"); boolean t7_2=st2.read()==(int)('Y'); if (t7_1&&t7_2){ mammal=true;hoof=true; Y rule7(mammal,hoof); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't mammal? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); boolean t8_1=st1.read()==(int)('Y'); System.out.println("the animal doesn't ruminate? In(Y/N)"); boolean t8_2=st2.read()==(int)('Y'); if (t8_1&&t8_2){ mammal=true;ruminate=true; rule8(mammal,ruminate); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't carnivore? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); boolean t9_1=st1.read()==(int)('Y'); System.out.println("the animal isn't snuffColor? In(Y/N)"); boolean t9_2=st2.read()==(int)('Y'); System.out.println("the animal isn't fuscousFleck? In(Y/N)"); boolean t9_3=st3.read()==(int)('Y'); if (t9_1&&t9_2&&t9_3){ carnivore=true;snuffColor=true;fuscousFleck=true; rule9(carnivore,snuffColor,fuscousFleck); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't carnivore? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); boolean t10_1=st1.read()==(int)('Y'); System.out.println("the animal isn't snuffColor? In(Y/N)"); boolean t10_2=st2.read()==(int)('Y'); System.out.println("the animal isn't blackStripe? In(Y/N)"); boolean t10_3=st3.read()==(int)('Y'); if (t10_1&&t10_2&&t10_3){ carnivore=true;snuffColor=true;blackStripe=true; rule10(carnivore,snuffColor,blackStripe); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't ungulate? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); BufferedInputStream st4= new BufferedInputStream (System.in); BufferedInputStream st5= new BufferedInputStream (System.in); boolean t11_1=st1.read()==(int)('Y'); System.out.println("the animal doesn't have longLeg? In(Y/N)"); boolean t11_2=st2.read()==(int)('Y'); System.out.println("the animal doesn't have longNeck? In(Y/N)"); boolean t11_3=st3.read()==(int)('Y'); System.out.println("the animal isn't snuffColor? In(Y/N)"); boolean t11_4=st4.read()==(int)('Y'); System.out.println("the animal isn't fuscousFleck? In(Y/N)"); boolean t11_5=st5.read()==(int)('Y'); if (t11_1&&t11_2&&t11_3&&t11_4&&t11_5){ ungulate=true;longLeg=true;longNeck=true;snuffColor=true;fuscousFleck=true; rule11(ungulate,longLeg,longNeck,snuffColor,fuscousFleck); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't ungulate? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); boolean t12_1=st1.read()==(int)('Y'); System.out.println("the animal isn't white ? In(Y/N)"); boolean t12_2=st2.read()==(int)('Y'); System.out.println("the animal isn't blackStripe? In(Y/N)"); boolean t12_3=st3.read()==(int)('Y'); if (t12_1&&t12_2&&t12_3){ ungulate=true;white=true;blackStripe=true; rule12(ungulate,white,blackStripe); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't aves? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); BufferedInputStream st4= new BufferedInputStream (System.in); BufferedInputStream st5= new BufferedInputStream (System.in); boolean t13_1=st1.read()==(int)('Y'); System.out.println("the animal isn't notFly? In(Y/N)"); boolean t13_2=st2.read()==(int)('Y'); System.out.println("the animal doesn't have longLeg? In(Y/N)"); boolean t13_3=st3.read()==(int)('Y'); System.out.println("the animal doesn't have longNeck? In(Y/N)"); boolean t13_4=st4.read()==(int)('Y'); System.out.println("the animal isn't blackAndWhite? In(Y/N)"); boolean t13_5=st5.read()==(int)('Y'); if (t13_1&&t13_2&&t13_3&&t13_4&&t13_5){ aves=true;notFly=true;longLeg=true;longNeck=true;blackAndWhite=true; rule13(aves,notFly,longLeg,longNeck,blackAndWhite); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't aves? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); BufferedInputStream st3= new BufferedInputStream (System.in); BufferedInputStream st4= new BufferedInputStream (System.in); boolean t14_1=st1.read()==(int)('Y'); System.out.println("the animal isn't notFly? In(Y/N)"); boolean t14_2=st2.read()==(int)('Y'); System.out.println("the animal doesn't swimming? In(Y/N)"); boolean t14_3=st3.read()==(int)('Y'); System.out.println("the animal doesn't have blackAndWhite? In(Y/N)"); boolean t14_4=st4.read()==(int)('Y'); if (t14_1&&t14_2&&t14_3&&t14_4){ aves=true;notFly=true;swimming=true;blackAndWhite=true; rule14(aves,notFly,swimming,blackAndWhite); } }catch(IOException e) { System.out.println("Error reading from user"); } System.out.println("continue observe"); System.out.println("the animal isn't aves? In(Y/N)"); try{ BufferedInputStream st1= new BufferedInputStream (System.in); BufferedInputStream st2= new BufferedInputStream (System.in); boolean t15_1=st1.read()==(int)('Y'); System.out.println("the animal isn't beGoodAtFly? In(Y/N)"); boolean t15_2=st2.read()==(int)('Y'); if (t15_1&&t15_2){ aves=true;beGoodAtFly=true; rule15(aves,beGoodAtFly); } }catch(IOException e) { System.out.println("Error reading from user"); } } /*主函数 */ public static void main (String[ ] args){ AnimalIdentify animal1=new AnimalIdentify(); animal1.queryUseRule(); }