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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孙小亚   /  2014-8-23 19:41  /  2916 人查看  /  27 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

顶!!!!!!!!
回复 使用道具 举报
lvc 中级黑马 2014-9-5 00:43:36
22#
  1. package exam;

  2. /*
  3. * 10、 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
  4. * 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不
  5. * 会发生这种问题。编程解决猫狗鱼过河问题。
  6. * */
  7. public class Test10 {
  8.         public static void main(String[] args)// throws Exception
  9.         {
  10.                 GoRiver goriver = GoRiver.getRiver();
  11.                 /*
  12.                  * goriver.nounRiver("猫"); goriver.nounRiver("狗");
  13.                  * goriver.southRiver("猫"); goriver.nounRiver("鱼");
  14.                  * goriver.nounRiver("猫"); goriver.getArray();
  15.                  */
  16.                 goriver.nounRiver("狗");
  17.                 goriver.southRiver("猫");
  18.                 goriver.nounRiver("鱼");
  19.                 goriver.nounRiver("猫");
  20.                 goriver.getArray();
  21.         }
  22. }

  23. class GoRiver {
  24.         private static GoRiver river = new GoRiver();

  25.         private GoRiver() {
  26.         }

  27.         public static GoRiver getRiver() {
  28.                 return river;
  29.         }

  30.         // 北岸
  31.         String[] nounRiverside = { "老农", "猫", "狗", "鱼" };
  32.         // 南岸
  33.         // String[] southRiverside = new String[4];
  34.         String[] southRiverside = { "null", "null", "null", "null" };
  35.         String[] arrayOne = { "猫", "狗" };
  36.         String[] arrayTwo = { "猫", "鱼" };
  37.         boolean exchengErray = true;

  38.         // 北岸到南岸
  39.         public void nounRiver(String str) {
  40.                 boolean excheng = false;
  41.                 int one = 0, two = 0;
  42.                 // System.out.print(str + "1");
  43.                 //固定用数组中的0位来存储老农
  44.                 for (int i = 0; i < nounRiverside.length; i++) {
  45.                         try {
  46.                                 if (nounRiverside[i].equals(str)) {
  47.                                         nounRiverside[i] = "null";
  48.                                         nounRiverside[0] = "null";
  49.                                         excheng = true;
  50.                                 }
  51.                         } catch (Exception e) {
  52.                                 System.out.print("...noun错误1...");
  53.                         }
  54.                         //判断北岸留下的是否符合要求
  55.                         for (int s = 0; s < 2; s++) {
  56.                                 if (arrayOne[s].equals(nounRiverside[i])) {
  57.                                         one++;
  58.                                 }
  59.                                 if (arrayTwo[s].equals(nounRiverside[i])) {
  60.                                         two++;
  61.                                 }
  62.                         }
  63.                         if (one == 2 || two == 2) {
  64.                                 excheng = false;
  65.                                 exchengErray = false;
  66.                                 System.out.println("北岸失败");
  67.                                 break;
  68.                         }
  69.                 }
  70.                 //当符合要求就把存入南岸,下同
  71.                 if (excheng) {
  72.                         try {
  73.                                 for (int j = 0; j < southRiverside.length; j++) {
  74.                                         // System.out.print(str + "2");
  75.                                         southRiverside[0] = "老农";
  76.                                         if (southRiverside[j].equals("null")) {
  77.                                                 southRiverside[j] = str;
  78.                                                 // System.out.print("southRiverside[j]"+southRiverside[j]);
  79.                                                 break;
  80.                                         }
  81.                                 }
  82.                         } catch (Exception e) {
  83.                                 System.out.print("...noun错误2...");
  84.                         }
  85.                 }
  86.         }

  87.         // 南岸到北岸
  88.         public void southRiver(String str) {
  89.                 boolean excheng = false;
  90.                 int one = 0, two = 0;
  91.                 // System.out.print(str + "3");
  92.                 for (int i = 0; i < southRiverside.length; i++) {
  93.                         try {
  94.                                 if (southRiverside[i].equals(str)) {
  95.                                         southRiverside[i] = "null";
  96.                                         southRiverside[0] = "null";
  97.                                         excheng = true;
  98.                                 }
  99.                         } catch (Exception e) {
  100.                                 System.out.print("...south错误1...");
  101.                         }
  102.                         // 判断南岸留下的是否符合要求
  103.                         for (int s = 0; s < 2; s++) {
  104.                                 if (arrayOne[s].equals(nounRiverside[i])) {
  105.                                         one++;
  106.                                 }
  107.                                 if (arrayTwo[s].equals(nounRiverside[i])) {
  108.                                         two++;
  109.                                 }
  110.                         }
  111.                         if (one == 2 || two == 2) {
  112.                                 excheng = false;
  113.                                 exchengErray = false;
  114.                                 System.out.println("南岸失败");
  115.                                 break;
  116.                         }
  117.                 }
  118.                 if (excheng) {
  119.                         try {
  120.                                 for (int j = 0; j < nounRiverside.length; j++) {
  121.                                         nounRiverside[0] = "老农";
  122.                                         if (nounRiverside[j].equals("null")) {
  123.                                                 nounRiverside[j] = str;
  124.                                                 break;
  125.                                         }
  126.                                 }
  127.                         } catch (Exception e) {
  128.                                 System.out.print("...south错误...");
  129.                         }
  130.                 }
  131.         }

  132.         public void getArray() {
  133.                 if (exchengErray) {
  134.                         for (int i = 0; i < southRiverside.length; i++) {
  135.                                 System.out.println(southRiverside[i] + "过河成功");

  136.                         }
  137.                 } else {
  138.                         System.out.println("过河失败");
  139.                 }
  140.         }

  141. }
复制代码
回复 使用道具 举报
lvc 中级黑马 2014-9-5 00:47:30
23#
自己写的,没有优化。还是没有突出面向对象编程。看了楼主代码,哎。。,与他相差太远。
回复 使用道具 举报
基础测试里有这个么?
回复 使用道具 举报
孙小亚 发表于 2014-9-2 22:46
首先知道要怎么才能过河,然后思考怎样设计类以及类的相关属性和方法。
我是这样设计的:
动物类Animal: ...

谢谢您~我自己在多琢磨琢磨O(∩_∩)O~
回复 使用道具 举报
xiayoutianxia 发表于 2014-9-5 08:58
基础测试里有这个么?

有的人的基础题里面有。
回复 使用道具 举报
挺有意思的
回复 使用道具 举报

:):):):):)
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马