本文主要是介绍农民带动物过河(狗猫鱼),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">备注:这个是黑马程序员的基础测试题,</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(255, 0, 0); background-color: rgb(255, 255, 255);">代码都是我自己亲手写的</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">,跟其他人的不同,大家可以来看看,给给建议什么的。。。</span>
如需转载麻烦表明出处,附上此贴链接,谢谢!
一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题。
代码:
import java.util.ArrayList;
import java.util.List;public class AnimalCrossRiver {private Bank srcBank;private Bank destBank;public AnimalCrossRiver(Bank srcBank, Bank destBank) {this.srcBank = srcBank;this.destBank = destBank;}public Bank getSrcBank() {return srcBank;}public Bank getDestBank() {return destBank;}public static void main(String[] args) {Bank srcBank = new Bank(initsrcBankAnimals(),true);Bank destBank = new Bank(new ArrayList<Animal>(), false);AnimalCrossRiver acr = new AnimalCrossRiver(srcBank, destBank);acr.riverCrossing();} public static List<Animal> initsrcBankAnimals(){List<Animal> srcBankAnimals = new ArrayList<Animal>();srcBankAnimals.add(new Animal("狗",null));srcBankAnimals.add(new Animal("猫","狗"));srcBankAnimals.add(new Animal("鱼","猫")); return srcBankAnimals;}private void riverCrossing(){ Bank srcBank = this.getSrcBank();Bank destBank = this.getDestBank();Animal temp1 = null;Animal temp2 = null;while(true){ temp1 = srcBank.removeAni();//函数中把此岸被设置没人 //对岸增加动物,自动设置对岸有人destBank.addAni(temp1); //打印信息System.out.println("河岸----------------->对岸(人在对岸)");System.out.println("河岸上动物个数:"+srcBank.getBankAnimals().size());System.out.print(srcBank.isHasHuman()? "有人:":"没人:"+" ");for(Animal animal:srcBank.getBankAnimals()){System.out.print(animal.getName()+" ");}System.out.println();System.out.println("对岸上动物个数:"+destBank.getBankAnimals().size());System.out.print(destBank.isHasHuman()? "有人:":"没人:"+" ");for(Animal animal:destBank.getBankAnimals()){System.out.print(animal.getName()+" ");}System.out.println(); if(destBank.getBankAnimals().size() == 3){ //对岸动物数量达到三个,结束System.out.println();System.out.println("-----全部通过河岸-----");break;}/****如果从对岸离开后对岸不安全,带走一个动物到河岸****/if(!destBank.noHumanisSafe()){temp2 = destBank.removeAni(); srcBank.addAni(temp2); //把从对岸带回来的动物添加到河岸,设置有人//打印信息System.out.println();System.out.println("对岸----------------->河岸()人在河岸");System.out.println("河岸上动物个数:"+srcBank.getBankAnimals().size());System.out.print(srcBank.isHasHuman()? "有人:":"没人:"+" ");for(Animal animal:srcBank.getBankAnimals()){System.out.print(animal.getName()+" ");}System.out.println();System.out.println("对岸上动物个数:"+destBank.getBankAnimals().size());System.out.print(destBank.isHasHuman()? "有人:":"没人:"+" ");for(Animal animal:destBank.getBankAnimals()){System.out.print(animal.getName()+" ");}System.out.println();}System.out.println();/*---测试用---*//* int i=1;* i++;* if(i == 5){* break;}*/}}
}//Bank类
class Bank{private List<Animal> bankAnimals;private boolean hasHuman;private boolean isSafe;//构造函数public Bank(List<Animal> bankAnimals, boolean hasHuman) {this.bankAnimals = bankAnimals;this.hasHuman = hasHuman;} //获取河岸的动物public List<Animal> getBankAnimals() {return bankAnimals;}//河岸是否有人public boolean isHasHuman() {return hasHuman;}public void setHasHuman(boolean hasHuman) {this.hasHuman = hasHuman;}//添加动物,自动设置对岸有人public void addAni(Animal animal){this.setHasHuman(true);this.getBankAnimals().add(animal);}//删除动物 public Animal removeAni(){this.setHasHuman(false);List<Animal> animals = this.getBankAnimals();Animal temp = null;for(int i=0;i<animals.size();i++){temp = animals.get(0);animals.remove(0);if(this.noHumanisSafe()){ //执行完此岸被设置没人return temp;}else{animals.add(temp);}}return null;}//如果人离开了是否安全,执行完此岸被设置没人public boolean noHumanisSafe(){this.setHasHuman(false);if(this.bankAnimals.size() == 3){return false;}if(this.bankAnimals.size() == 2){if(this.bankAnimals.get(0).getName() == this.bankAnimals.get(1).getNaturalEnemy()||this.bankAnimals.get(1).getName() == this.bankAnimals.get(0).getNaturalEnemy()){return false;}}return true;}
}
//动物类
class Animal{// 动物名称private String name;// 动物的天敌private String naturalEnemy; public Animal(String name, String naturalEnemy) {this.name = name;this.naturalEnemy = naturalEnemy;}public String getName() {return name;}public String getNaturalEnemy() {return naturalEnemy;}
}
代码经过测试,没有问题。下面是截图:
谢谢阅读,欢迎交流!
这篇关于农民带动物过河(狗猫鱼)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!