不用那么长代码的,亲们。
这晚我刚刚写的,讨论学习。
- import java.util.LinkedList;
- public class Ferry {
- public static void main(String[] args) {
- LinkedList<String> here = new LinkedList<String>();
- LinkedList<String> there = new LinkedList<String>();
- String cat = "猫";
- String dog = "狗";
- String fish = "鱼";
- int count = 0;
- here.add(cat);
- here.add(dog);
- here.add(fish);
- while(there.size()<3){
- System.out.println("----第"+(++count)+"次摆渡开始----");
- String boat = null;
- while(true){
- boat = here.removeFirst();
- if(isNice(here)){
- there.addLast(boat);
- System.out.println("老农正把("+boat+")送往河对面....");
- break;
- }
- here.addLast(boat);
- }
- if(!isNice(there)&&there.size()<3){
- boat = there.removeFirst();
- System.out.println("并把("+boat+")往回送....");
- here.addLast(boat);
- }
- System.out.println("河这面:"+here);
- System.out.println("河对面:"+there);
- }
- }
- public static boolean isNice(LinkedList<String> li){
- if(li.contains("猫")&&li.contains("鱼"))
- return false;
- else if(li.contains("狗")&&li.contains("猫"))
- return false;
- else
- return true;
- }
- }
复制代码 |