A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈圳 高级黑马   /  2013-3-21 11:49  /  1965 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

早上起来得早,便逛了下论坛,然后看到现在论坛上现在越来越多人为了技术分不断提问...
提的还都是百度里一些比较老的练习题类,我对这种做法总是不赞同的.但是看到了问题,总有些想做做的冲动.
我测试过,结果是正确的.但感觉有些问题,由于要开始入学测试了,也没有时间去优化了,发出来大家交流一下.
  1. public class example1 {
  2.         /*老伯伯要带鱼、狗、猫过河到
  3.         对岸.,有一条船,只能坐一个人,
  4.         老伯每次只能带一样动物过河,当
  5.         老伯不在的时侯狗会咬猫,猫会吃
  6.         鱼,鱼会跳下河,请问怎么顺序过河呢?要求:编
  7.         写程序,由程序来推出过河的顺序*/
  8.         public static void main(String[] args) {
  9.                 Animal animal =Animal.CAT;
  10.                 River river=new River();
  11.                 while(!(river.getCount()==3)){
  12.                         river.Cross(animal);
  13.                         animal=animal.nextAnimal();
  14.                         }
  15.                 System.out.println("安全过河了泥沙河,oh year!");
  16.                 }
  17. }
  18. enum Animal{
  19.         FISH,CAT,DOG,BOSS;
  20.         public boolean animalDie(String coexist){
  21.                 if(this.toString()=="鱼"&&coexist=="猫"||this.toString()=="猫"&&coexist=="鱼")
  22.                         return false;
  23.                 else if(this.toString()=="猫"&&coexist=="狗"||this.toString()=="狗"&&coexist=="猫")
  24.                         return false;
  25.                 else return true;
  26.         }
  27.         public Animal nextAnimal(){
  28.                 switch(this){
  29.                         case FISH:
  30.                                 return CAT;
  31.                         case CAT:
  32.                                 return DOG;
  33.                         case DOG:
  34.                                 return FISH;
  35.                 }
  36.                 return null;
  37.         }
  38.         public String toString(){
  39.                 switch(this){
  40.                         case FISH:
  41.                                 return "鱼";
  42.                         case CAT:
  43.                                 return "猫";
  44.                         case DOG:
  45.                                 return "狗";
  46.                 }
  47.                 return null;
  48.         }
  49. }
  50. class River{
  51.         private Animal animal;
  52.         private String[] animalName=new String[3];
  53.         private int count=0;
  54.         public void Cross(Animal ail){
  55.                 animal=ail;
  56.                 System.out.println(ail.toString()+"兄过河啦!");
  57.                 if(count<3){//避免角标越界
  58.                         animalName[count]=ail.toString();
  59.                         System.out.println(animalName[count]);
  60.                 }
  61.                 if(bank()==true);
  62.                         count++;
  63.         }
  64.         public boolean bank(){
  65.                 System.out.print("现在河对岸有"+(count+1)+"只动物:");
  66.                 for(int i=0;i<count+1;i++)
  67.                         System.out.print(animalName[i]+" ");
  68.                 System.out.println();
  69.                 if(count==1){
  70.                         if(!(animal.animalDie(animalName[0]))){
  71.                                 System.out.println("尼玛"+animal.toString()+"与"+animalName[0]+"打架啦!");
  72.                                 System.out.println(animalName[0]+"被带回去了!");
  73.                                 animalName[0]=animal.toString();
  74.                                 count--;
  75.                                 return false;
  76.                         }
  77.                 }
  78.                 return true;
  79.         }
  80.         public int getCount() {
  81.                 return count;
  82.         }
  83.        
  84. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马