java编写动物乐园_编写动物乐园java,满意会加分的!!!

展开全部

public class Animal

{

public String name;

public int legs;

public String sound;

public Animal(String name, int legs, String sound){

e68a8462616964757a686964616f31333332623262this.name = name;

this.legs = legs;

this.sound = sound;

}

public String toString(){

return name + "\t" + legs + "\t" + sound;

}

public static void main(String[]args)

{

//init

Animal[] animals = new Animal[3];

animals[0] = new Animal("加菲猫",4, "喵喵喵");

animals[1] = new Animal("唐小鸭",2, "嘎嘎嘎");

animals[2] = new Animal("海豚奇奇",0, "海豚音");

byte[] b = new byte[1024];

String[] names = {"猫", "鸭子", "海豚"};

int[] legs = {4,2,0};

String name;

int leg;

String sound;

while(true){

try{

System.out.println("动物名称\t腿的条数\t动物叫");

for (int i = 0; i 

System.out.println(animals[i].toString());

}

System.out.println("0-修改,其他数字-退出");

int n = System.in.read(b);

String s = new String(b,0,n);

int oper = Integer.parseInt(s.trim());

if (oper != 0) {

System.out.println("退出!");

return;

}

for (int i = 0; i 

System.out.println("请输入"+names[i]+"的名字");

n = System.in.read(b);

s = new String(b,0,n);

name = s.trim();

if (name.length() == 0) {

throw new Exception(names[i]+"必须有名字");

}

System.out.println("请输入"+names[i]+"的腿数");

n = System.in.read(b);

s = new String(b,0,n);

leg = Integer.parseInt(s.trim());

if (leg != legs[i]) {

throw new Exception(names[i]+"必须有"+legs[i]+"条腿");

}

System.out.println("请输入"+names[i]+"的叫声");

n = System.in.read(b);

s = new String(b,0,n);

sound = s.trim();

if (sound.length() == 0) {

throw new Exception(names[i]+"必须有声音");

}

animals[i].name = name;

animals[i].legs = leg;

animals[i].sound = sound;

}

}

catch(Exception e){

e.printStackTrace();

}

}

}

}

凑合写的....

instanceof 不难吧...就是验证类是否为类或者父类的实例...

我看你ppt要求的是自定义异常抛出,接口继承应该很简单,抛出异常参考我的代码即可..

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值