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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 陈兆辉 于 2014-9-23 10:32 编辑

测试题,猫狗鱼过河问题。
我写了内部类,因为感觉过河和判断安全都是老农的动作,应该是老农的类提供。但是写完了,看网上其他人写的,没有这么做的,都是这届放到主函数里面。我这会不会有问题啊?
  1. package com.itheima;
  2. import java.util.*;

  3. public class Test10 {
  4.         //定义一个老农的内部类,拥有一系列的方法,为了给主函数调用,使用static修饰
  5.         static class Peasant {
  6.                 //过河方法,接收河两岸(起点和终点)两个存储动物的集合
  7. void acrossRiver(List<String> starting,List<String> destination){
  8.                         while(true){
  9.                                 //定义一个变量存储老农携带的动物
  10.                                 String animal;
  11.                                 int index;
  12.                                 //从起点集合中选择一个动物,直到确保剩余的动物安全时停止循环
  13.                                 do{
  14.                                         index = new Random().nextInt(starting.size());//产生一个随机数
  15.                                         animal = starting.remove(index);
  16.                                         System.out.println("starting:老农选择了"+animal);
  17.                                 }while(!isSafe(starting,animal));
  18.                                 System.out.println("老农最终选择"+animal+"一起过河");
  19.                                 //将所带的动物放到终点的动物集合中。
  20.                                 destination.add(animal);
  21.                                 animal = null;
  22.                                 //如果终点的动物集合中存了3个动物,则结束循环
  23.                                 if(destination.size()==3){
  24.                                         System.out.println("老农成功过河!");
  25.                                         break;
  26.                                 }
  27.                                 //判断对岸是否安全,安全则独自返回,不安全则带一个动物返回,直至安全。
  28.                                 if(!isSafe(destination,animal)){ //此处传入的animal为null,避免重复加入某个动物的错误
  29.                                        
  30.                                         System.out.println("destination:对岸不安全,老农准备带一个动物返回");
  31.                                         do{
  32.                                                 index = new Random().nextInt(destination.size());
  33.                                                 animal = destination.remove(index);
  34.                                                 System.out.println("老农准备带"+animal+"返回");
  35.                                         }while(!isSafe(destination,animal));
  36.                                         System.out.println("老农最终选择了"+animal+"一起返回");
  37.                                         starting.add(animal);//返回,将携带的动物放回起点集合
  38.                                 }
  39.                                 else{
  40.                                         System.out.println("对岸安全,老农独自返回。");
  41.                                 }
  42.                         }
  43.                 }
  44.                
  45.                 boolean isSafe(List<String> animals,String animal){
  46.                         boolean safe = true;
  47.                         //如果不安全,把标记改为假
  48.                         if(animals.contains("cat") && animals.size()>1){
  49.                                 //如果携带了动物,就把动物放回去。
  50.                                 if(!(animal==null))
  51.                                         animals.add(animal);
  52.                                 safe = false;
  53.                         }
  54.                         return safe;
  55.                 }
  56.                
  57.         }
  58.         
  59.         //主函数,创建起点动物集合,添加三种动物进去。
  60.         public static void main(String args[]){
  61.                 Peasant peasant = new Peasant();
  62.                 List<String> starting = new ArrayList<String>();
  63.                 starting.add("dog");
  64.                 starting.add("cat");
  65.                 starting.add("fish");
  66.                 peasant.acrossRiver(starting, new ArrayList<String>());
  67.         }
  68.         
  69. }
复制代码



2 个回复

倒序浏览
不知道咋整的,格式有点问题,不好意思了,将就一下吧。
回复 使用道具 举报
果断一直没人理我啊。纠结
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马