- package exam;
- /*
- * 10、 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
- * 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不
- * 会发生这种问题。编程解决猫狗鱼过河问题。
- * */
- public class Test10 {
- public static void main(String[] args)// throws Exception
- {
- GoRiver goriver = GoRiver.getRiver();
- /*
- * goriver.nounRiver("猫"); goriver.nounRiver("狗");
- * goriver.southRiver("猫"); goriver.nounRiver("鱼");
- * goriver.nounRiver("猫"); goriver.getArray();
- */
- goriver.nounRiver("狗");
- goriver.southRiver("猫");
- goriver.nounRiver("鱼");
- goriver.nounRiver("猫");
- goriver.getArray();
- }
- }
- class GoRiver {
- private static GoRiver river = new GoRiver();
- private GoRiver() {
- }
- public static GoRiver getRiver() {
- return river;
- }
- // 北岸
- String[] nounRiverside = { "老农", "猫", "狗", "鱼" };
- // 南岸
- // String[] southRiverside = new String[4];
- String[] southRiverside = { "null", "null", "null", "null" };
- String[] arrayOne = { "猫", "狗" };
- String[] arrayTwo = { "猫", "鱼" };
- boolean exchengErray = true;
- // 北岸到南岸
- public void nounRiver(String str) {
- boolean excheng = false;
- int one = 0, two = 0;
- // System.out.print(str + "1");
- //固定用数组中的0位来存储老农
- for (int i = 0; i < nounRiverside.length; i++) {
- try {
- if (nounRiverside[i].equals(str)) {
- nounRiverside[i] = "null";
- nounRiverside[0] = "null";
- excheng = true;
- }
- } catch (Exception e) {
- System.out.print("...noun错误1...");
- }
- //判断北岸留下的是否符合要求
- for (int s = 0; s < 2; s++) {
- if (arrayOne[s].equals(nounRiverside[i])) {
- one++;
- }
- if (arrayTwo[s].equals(nounRiverside[i])) {
- two++;
- }
- }
- if (one == 2 || two == 2) {
- excheng = false;
- exchengErray = false;
- System.out.println("北岸失败");
- break;
- }
- }
- //当符合要求就把存入南岸,下同
- if (excheng) {
- try {
- for (int j = 0; j < southRiverside.length; j++) {
- // System.out.print(str + "2");
- southRiverside[0] = "老农";
- if (southRiverside[j].equals("null")) {
- southRiverside[j] = str;
- // System.out.print("southRiverside[j]"+southRiverside[j]);
- break;
- }
- }
- } catch (Exception e) {
- System.out.print("...noun错误2...");
- }
- }
- }
- // 南岸到北岸
- public void southRiver(String str) {
- boolean excheng = false;
- int one = 0, two = 0;
- // System.out.print(str + "3");
- for (int i = 0; i < southRiverside.length; i++) {
- try {
- if (southRiverside[i].equals(str)) {
- southRiverside[i] = "null";
- southRiverside[0] = "null";
- excheng = true;
- }
- } catch (Exception e) {
- System.out.print("...south错误1...");
- }
- // 判断南岸留下的是否符合要求
- for (int s = 0; s < 2; s++) {
- if (arrayOne[s].equals(nounRiverside[i])) {
- one++;
- }
- if (arrayTwo[s].equals(nounRiverside[i])) {
- two++;
- }
- }
- if (one == 2 || two == 2) {
- excheng = false;
- exchengErray = false;
- System.out.println("南岸失败");
- break;
- }
- }
- if (excheng) {
- try {
- for (int j = 0; j < nounRiverside.length; j++) {
- nounRiverside[0] = "老农";
- if (nounRiverside[j].equals("null")) {
- nounRiverside[j] = str;
- break;
- }
- }
- } catch (Exception e) {
- System.out.print("...south错误...");
- }
- }
- }
- public void getArray() {
- if (exchengErray) {
- for (int i = 0; i < southRiverside.length; i++) {
- System.out.println(southRiverside[i] + "过河成功");
- }
- } else {
- System.out.println("过河失败");
- }
- }
- }
复制代码 |