考试题目的思考方式错误

题目:有两个动物,一只猴子,一头大象,

            两种水果,香蕉和桃子,

            猴子吃到好的桃子笑哈哈,吃到坏的哭起来.

            吃到好的香蕉跳起来,吃到坏的香蕉扔掉香蕉,

           大象不吃桃子,

           大象吃到好的香蕉翘鼻子,

           吃到坏的香蕉吐出来

正常来说应该是用继承来写这道题,能方便拓展。

public class Last {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("欢迎来到动物园,您想喂猴子还是大象:\n 1 猴子  2 大象");

int dw = sc.nextInt();

System.out

.println("欢迎来到动物园,您想喂什么水果:\n 1 好的桃子  2 坏的桃子 \n 3 好的香蕉 4 坏的香蕉");

int num = sc.nextInt();

动物 dongwu = null;

if (dw == 1) {

dongwu = new 猴子();

} else if (dw == 2) {

dongwu = new 大象();

}

水果 sg = null;

if (num == 1) {

sg = new 桃子("好的");

}else if(num==2){

sg = new 桃子("坏的");

}else if(num==3){

sg = new 香蕉("好的");

}else if(num==4){

sg = new 香蕉("坏的");

}

dongwu.吃(sg);

}

}

class 水果 {

String type;

}

class 香蕉 extends 水果 {

public 香蕉(String type) {

this.type = type;

}

}

class 桃子 extends 水果 {

public 桃子(String type) {

this.type = type;

}

}

class 动物 {

void 吃(水果 sg) {

}

}

class 猴子 extends 动物 {

void 吃(水果 sg) {

if (sg instanceof 香蕉) {

if (sg.type.equals("好的")) {

System.out.println("猴子高兴的跳起来");

} else {

System.out.println("猴子扔掉坏的香蕉");

}

} else if (sg instanceof 桃子) {

if (sg.type.equals("好的")) {

System.out.println("猴子高兴的笑哈哈");

} else {

System.out.println("猴子伤心的哭起来");

}

}

}

}

class 大象 extends 动物 {

void 吃(水果 sg) {

if (sg instanceof 香蕉) {

if (sg.type.equals("好的")) {

System.out.println("翘鼻子");

} else {

System.out.println("吐出来");

}

} else if (sg instanceof 桃子) {

System.out.println("不吃桃子");

}

}

}

但是,考试前学的是try catch,所以当时想学到什么用什么,用的是try catch来写的。

import java.util.Random;

import java.util.Scanner;

public class Demo3 {

public static void main(String[] args) {

//String a[]={"好香蕉","好桃子","坏香蕉","坏桃子"};

Random rd = new Random();

//int index1 = rd.nextInt(4);

//int index2 = rd.nextInt(4);

int t = -1;

int x = -1;

int z = -1;

//Demo de = new Demo();

//de.Monkey(a[index1]);

//de.Telephone(a[index2]);

System.out.println("0代表大象,1代表猴子");

Scanner sc1 = new Scanner(System.in); 

System.out.println("选择的动物是:");

do{

try{

// 获得输入的数字

x = sc1.nextInt();

}catch (Exception ex) {

System.out.println("只有大象和猴子两种动物,必须输入0或者1,请重新输入您选择动物代表的数字:");

sc1 = new Scanner(System.in);

continue;

}

if(x==0){

        System.out.println("0代表好香蕉,1代表好桃子,2代表坏香蕉,3代表坏桃子");

        Scanner sc = new Scanner(System.in); 

        System.out.println("给大象:");

        do{

try {

// 获得输入的数字

t = sc1.nextInt();

} catch (Exception ef) {

System.out.println("必须输入0~3的数字,请重新输入您选择给动物代表食物的数字:");

sc1 = new Scanner(System.in);

continue;

}

if (t==0) {

System.out.println("大象鼻子翘起来");

} else if (t==1) {

System.out.println("大象不吃桃子");

}else if (t==2) {

System.out.println("大象把烂香蕉吐出来");

}else if (t==3) {

System.out.println("大象不吃桃子");

}

} while(t>3||t<0);

}else if(x==1){

       System.out.println("0代表好香蕉,1代表好桃子,2代表坏香蕉,3代表坏桃子");  

       System.out.println("给猴子:");

       Scanner sc2 = new Scanner(System.in); 

       do{

try {

// 获得输入的数字

z = sc2.nextInt();

} catch (Exception ef) {

System.out.println("必须输入0~3的数字,请重新输入您选择给动物代表食物的数字:");

sc2 = new Scanner(System.in);

continue;

}

if (t==0) {

System.out.println("猴子跳起来");

} else if (t==1) {

System.out.println("猴子笑哈哈");

}else if (t==2) {

System.out.println("猴子把烂香蕉扔掉出来");

}else if (t==3) {

System.out.println("猴子哭出来");

}

} while(t>3||t<0);

}

}while(x>1||x<0);

}

用这个方法写,可以是可以,但是写成功后,自己也发现这个方法很麻烦,一不小心就容易出错,而且要经行拓展的话很麻烦,完全没有继承方法的好,这也是一个思想方式,但是还是要用简单简洁的方式才会更好,以后遇见类似问题也需要自己的用到继承这种简单方便拓展的方式!

猜你喜欢

转载自hxquicl.iteye.com/blog/2268385