请老师看看根据任务要求哪些地方不对,需要怎么改正?

请老师看看根据任务要求哪些地方不对,需要怎么改正?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.imooc.animal;
 
public abstract class Animal {
    private String name;// 昵称
    private int age;// 年龄
 
    public abstract void love();// 喜好抽象
 
    public Animal() {
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public Animal(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }
 
}
1
2
3
4
5
6
package com.imooc.animal;
 
public interface IAct {
  void skill();//技能
  void act();//描述表演
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.imooc.animal;
 
public class Bear extends Animal implements IAct {
 
    public Bear(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }
 
    public Bear() {
 
    }
 
    @Override
    public void skill() {
        System.out.println("技能:挽着花篮,打着雨伞,自立走秀");
 
    }
 
    @Override
    public void love() {
        System.out.println("爱好:喜欢卖萌");
    }
 
    @Override
    public void act() {
        System.out.println("表演者:" this.getName());
        System.out.println("年龄:" this.getAge() + "岁");
        skill();
        love();
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.imooc.animal;
 
public class Lion extends Animal implements IAct {
    private String color;// 毛色
    private String sex;// 性别
 
    public Lion() {
    }
 
    public String getColor() {
        return color;
    }
 
    public void setColor(String color) {
        this.color = color;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public Lion(String name, int age, String color, String sex) {
        this.setName(name);
        this.setAge(age);
        this.setColor(color);
        this.setSex(sex);
    }
 
    @Override
    public void skill() {
        System.out.println("技能:擅长钻火圈");
    }
 
    @Override
    public void love() {
        System.out.println("爱好:喜欢吃各种肉类");
    }
 
    @Override
    public void act() {
        System.out.println("表演者:" this.getName());
        System.out.println("年龄:" this.getAge() + "岁");
        System.out.println("性别:" this.getSex());
        System.out.println("毛色:" this.getColor());
        skill();
        love();
 
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.imooc.animal;
 
public class Monkey extends Animal implements IAct {
    private String type;//品种
public Monkey() {
}
public Monkey(String name,int age,String Type) {
    this.setName(name);
    this.setAge(age);
    this.setType(Type);
}
 
    public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
    @Override
    public void skill() {
        System.out.println("技能:骑独轮车过独木桥");
         
    }
 
    @Override
    public void love() {
        System.out.println("爱好:喜欢模仿人的动作表情");
    }
    @Override
    public void act() {
        System.out.println("表演者:" this.getName());
        System.out.println("年龄:" this.getAge() + "岁");
        System.out.println("品种:" this.getType());
        skill();
        love();
         
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.imooc.animal;
 
public class Parrot extends Animal implements IAct {
    private String type;//品种
 
    public Parrot() {
 
    }
 
    public Parrot(String name, int age, String type) {
        this.setName(name);
        this.setAge(age);
        this.setType(type);
    }
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    @Override
    public void skill() {
        System.out.println("技能:擅长模仿");
 
    }
 
    @Override
    public void love() {
        System.out.println("爱好:喜欢吃坚果和松子");
 
    }
 
    @Override
    public void act() {
        System.out.println("表演者:" this.getName());
        System.out.println("年龄:" this.getAge() + "岁");
        System.out.println("品种:" this.getType());
        skill();
        love();
 
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.imooc.animal;
 
public class Clown extends Animal implements IAct {
    private int years;// 艺龄
 
    public Clown() {
 
    }
 
    public Clown(String name, int years) {
        this.setName(name);
        this.setYears(years);
    }
 
    public int getYears() {
        return years;
    }
 
    public void setYears(int years) {
        this.years = years;
    }
 
    public void dress() {
        System.out.println("着装:身穿五彩服装,头上戴着彩的色帽子,脸上画着夸张的彩妆");
    }
 
    @Override
    public void skill() {
        System.out.println("技能:脚踩高跷,进行杂技魔术表演");
 
    }
 
    @Override
    public void love() {
        // TODO 自动生成的方法存根
 
    }
 
    @Override
    public void act() {
        System.out.println("表演者:" this.getName());
        System.out.println("艺龄:" this.getYears() + "年");
        dress();
        skill();
 
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.imooc.test;
 
import java.util.InputMismatchException;
import java.util.Scanner;
 
import com.imooc.animal.Bear;
import com.imooc.animal.Clown;
import com.imooc.animal.IAct;
import com.imooc.animal.Lion;
import com.imooc.animal.Monkey;
import com.imooc.animal.Parrot;
 
public class Test {
    /**
     
     * 提示信息
     */
    public void displayMenu() {
        System.out.println("******欢迎来到太阳马戏团******");
        System.out.println("**********请选择表演者**********");
        System.out.println("**********    1、棕熊       **********");
        System.out.println("**********    2、狮子       **********");
        System.out.println("**********    3、猴子       **********");
        System.out.println("**********    4、鹦鹉       **********");
        System.out.println("**********    5、小丑       **********");
 
    }
/**
 * 观看提示选项
 */
    public void prompt() {
        Scanner sc = new Scanner(System.in);
        int c = 0;
        System.out.println();
        System.out.println("******   是否继续观看(1/0)   ******");
        c = sc.nextInt();
        if (c == 1) {
            return;
        else {
            if (c == 0) {
                System.out.println("*******      欢迎下次光临      ******");
                return;
            }
        }
    }
 
    public static void main(String[] args) {
        Test menu = new Test();
        Scanner sc = new Scanner(System.in);
        int ab = 0;
        while (true) {
            menu.displayMenu();
            try {
                ab = sc.nextInt();
            catch (InputMismatchException e) {
                System.out.println("输入的数据格式有误,不能有非数字!请重新选择需要观看的节目");
                sc.next();
                continue;
            }
            switch (ab) {
            case 1:
                if (sc != null) {
                    IAct one = new Bear("Bill"1);
                    one.act();
                    menu.prompt();
                }
                break;
            case 2:
                if (sc != null) {
                    IAct two = new Lion("Lain"2"灰色""公狮");
                    two.act();
                    menu.prompt();
                }
                break;
            case 3:
                if (sc != null) {
                    IAct three = new Monkey("Tom"1"金丝猴");
                    three.act();
                    menu.prompt();
                }
                break;
            case 4:
                if (sc != null) {
                    IAct four = new Parrot("Rose"1"牡丹鹦鹉");
                    four.act();
                    menu.prompt();
                }
                break;
            case 5:
                if (sc != null) {
                    IAct fives = new Clown("Kahle"5);
                    fives.act();
                    menu.prompt();
                }
                return;
            default:
                System.out.println("输入的值必须是1-5范围的值!");
 
            }
        }
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

4回答

首先1,非常抱歉,之前老师给同学的截图中第一个红框的地方多写了break。建议去掉

2、关于输入英文后让用户重新输入,可以和输入菜单时的处理方法一致,如下图第二个红框所示,放在try里,如果输入非数字,则抛出异常,执行catch中的代码。

http://img1.sycdn.imooc.com//climg/5d2d6256000187a909110457.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


  • 鏡花水月_ 提问者 #1
    这样的话在输入1-5的时候如果输非1-5就直接进入是否观看1/0了 要怎么改呢
    2019-07-16 13:59:27
  • 鏡花水月_ 提问者 #2
    ******欢迎来到太阳马戏团****** **********请选择表演者********** ********** 1、棕熊 ********** ********** 2、狮子 ********** ********** 3、猴子 ********** ********** 4、鹦鹉 ********** ********** 5、小丑 ********** 7 输入的值必须是1-5范围的值! ******是否继续观看(1/0)******
    2019-07-16 14:00:06
好帮手慕阿莹 2019-07-16 14:20:28

同学你好,如果想实现同学所想,应该让它跳过后边代码,执行下次循环中的内容。

可以用continue来实现。例如:

http://img1.sycdn.imooc.com//climg/5d2d6ca10001115205420161.jpg



如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


好帮手慕阿莹 2019-07-16 11:18:54

1、同学你好,同学是否想问,如何使让用户输入是否继续的时候,输入除0和1之外的数字外,重新展示这个继续输入的菜单?

例如下图所示:

http://img1.sycdn.imooc.com//climg/5d2d3c89000173d903810206.jpg

如果是的:

首先,我们要想,既然这样,说明它有个循环判断的过程,,如此,我们便把该方法先注释掉:

http://img1.sycdn.imooc.com//climg/5d2d3cdf0001d4da09280432.jpg

http://img1.sycdn.imooc.com//climg/5d2d3cfd0001569f07640774.jpg

我们在switch结构的下边,外层while循环的里边,写一个小循环,当用户输入对了,则跳出该循环,如果输入不对,则一直在此循环中。

http://img1.sycdn.imooc.com//climg/5d2d3eb00001368010050738.jpg

2、

为了体现多态,不建议去直接创建实现类的对象,应该先声明一个接口的引用,如:IAct actor=null;然后在case 语句后面进行对象的创建,如case 1后的代码为:actor= new Bear("Bill", 1);  在default 中将actor置为null。
最后act方法的调用写在switch结构的外面,即即if(actor!=null){actor.act(); }
从上述的流程上看,通过接口的引用指向了子类的对象,最后去调用act()方法,因为actor引用指向的对象不同,调用act()方法就会输出不同的结果。这就是多态的体现。

http://img1.sycdn.imooc.com//climg/5d2d417000015d2a07700580.jpg

http://img1.sycdn.imooc.com//climg/5d2d418b000132db08800456.jpg

3、Clown不需要继承Animal,只实现IAct接口即可。

4、同学这里不用判断if(sc!=null)因为在if中咱们没有调用 sc呦,并且,sc 是Scanner对象,一般也不会为null呦。

5、之前给同学带来不好的学习体验感到非常抱歉,也建议同学可以在提问的时候,也描述一下同学的疑惑,也便于老师更好的定位同学的疑惑。再次感到十分抱歉。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 鏡花水月_ #1
    我问的根据任务要求哪些地方不对,需要怎么改正。。我要知道哪不对了还问什么?
    2019-07-16 11:58:04
  • 好帮手慕阿莹 回复 提问者 鏡花水月_ #2
    好的,老师理解同学的心情,请问老师如上的回答,是否解答了同学的疑惑呢?
    2019-07-16 12:03:04
  • 提问者 鏡花水月_ 回复 好帮手慕阿莹 #3
    老师那个在1/0里面数英文的时候提示错误重新输入改怎么写呢
    2019-07-16 13:30:00
吃吃吃鱼的猫 2019-07-15 16:52:18

同学你好,如图所示,case 5的分支中,需要使用break退出switch-case分支判断,不能使用return;

return;代表的是退出该方法~

http://img1.sycdn.imooc.com//climg/5d2c3e0700011e1504730164.jpg

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 提问者 鏡花水月_ #1
    你这就是应付了事的吧?
    2019-07-16 08:12:53
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码