请问主方法里是这样写吗

package Zoo;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal animal=new Bear();
IACT iact1=new Bear("bill",2);
IACT iact2=new Lion("Laian",2,"灰","公");
IACT iact3=new Monkey("Tom",1,"金丝猴");
IACT iact4=new Parrot("rose",1,"牡丹鹦鹉");
IACT iact5=new Clown("kandle",5);
Scanner sc=new Scanner(System.in);
animal.show();
while(true) {
int n=sc.nextInt();
switch(n) {
case 1:
iact1.act();
System.out.println("=================");
animal.show1();
int k=sc.nextInt();
while(k!=1&k!=0) {
animal.show3();
animal.show1();
k=sc.nextInt();
}
if(k==0) {
System.out.println("退出程序");
System.exit(0);
}
if(k==1)
animal.show();
break;
case 2:
iact2.act();
System.out.println("=================");
animal.show1();
int m=sc.nextInt();
while(m!=1&m!=0) {
animal.show3();
animal.show1();
m=sc.nextInt();
}
if(m==0) {
System.out.println("退出程序");
System.exit(0);
}
if(m==1)
animal.show();
break;
case 3:
iact3.act();
System.out.println("=================");
animal.show1();
int j=sc.nextInt();
while(j!=1&j!=0) {
animal.show3();
animal.show1();
k=sc.nextInt();
}
if(j==0) {
System.out.println("退出程序");
System.exit(0);
}
if(j==1)
animal.show();
break;
case 4:
System.out.println("=================");
animal.show1();
int l=sc.nextInt();
while(l!=1&l!=0) {
animal.show3();
animal.show1();
k=sc.nextInt();
}
if(l==0) {
System.out.println("退出程序");
System.exit(0);
}
if(l==1)
animal.show();
iact4.act();
break;
case 5:
System.out.println("=================");
animal.show1();
int a=sc.nextInt();
while(a!=1&a!=0) {
animal.show3();
animal.show1();
k=sc.nextInt();
}
if(a==0) {
System.out.println("退出程序");
System.exit(0);
}
if(a==1)
animal.show();
iact5.act();
break;
}
}
}
}
能运行但感觉好蠢啊 不知道怎么把中间那个过程简化的只用写一遍
正在回答 回答被采纳积分+1
package com.imooc.test;
import java.util.Scanner;
import com.imooc.model.Bear;
import com.imooc.model.Clown;
import com.imooc.model.IAct;
import com.imooc.model.Lion;
import com.imooc.model.Monkey;
import com.imooc.model.Parrot;
public class Test {
public static void main(String[] args) {
boolean flag = true;
while (flag) {
// 展示表演菜单
playList();
// 接收用户选择输入
Scanner input = new Scanner(System.in);
boolean actorFlay = true;
int index = 0;
// 根据用户输入产生表演者
while (actorFlay) {
index = input.nextInt();
IAct actor = createActor(index);
if (actor != null) {
//结束关于表演者产生的循环,并调用对应的表演方法
actorFlay = false;
actor.act();
System.out.println();
}else{
System.out.println("** 输入信息不正确,请重新输入 **");
playList();
}
}
// 提醒是否继续观看表演
boolean nextFlag = true;
while (nextFlag) {
System.out.println("****** 是否继续观看(1/0) ******");
index = input.nextInt();
switch (index) {
case 1:
nextFlag = false;
break;
case 0:
System.out.println("******* 欢迎下次光临 *******");
return;
default:
System.out.println("** 输入信息不正确,请重新输入 **");
break;
}
}
}
}
/**
* 表演菜单方法
*/
private static void playList() {
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、小丑 ***********");
}
/**
* 生成表演者
*
* @param index
* 根据菜单选择表演者序号
* @return 产生的表演者对象
*/
private static IAct createActor(int index) {
IAct actor = null;
switch (index) {
case 1:
actor = new Bear("Bill",1);
break;
case 2:
actor = new Lion("Lain",2,"公狮","灰色");
break;
case 3:
actor = new Monkey("Tom",1,"金丝猴");
break;
case 4:
actor = new Parrot("Rose",1,"牡丹鹦鹉");
break;
case 5:
actor = new Clown("Kahle",5);
break;
}
return actor;
}
}你可以参考 一下我的代码。我觉得你可以再看看视频。理解了就都好办了。不用着急做作业。
1、为了体现多态,不建议去直接创建实现类的对象,应该先声明一个接口的引用,如:IAct actor=null;然后在case 语句后面进行对象的创建,如case 1后的代码为:actor= new Bear("Bill", 1);
最后act方法的调用写在switch结构的外面,即即if(actor!=null){actor.act(); }
从上述的流程上看,通过接口的引用指向了子类的对象,最后去调用act()方法,因为actor引用指向的对象不同,调用act()方法就会输出不同的结果。这就是多态的体现。
2、 当选择表演项用户输入错误的时候,应该提示用户重新输入,并重新展示菜单,当问用户是否继续的时候,如果用户输入的不是1或0,应给出错误提示,让用户重新输入,直到用户输入0或1为止。
经过上边的改写,可以用这样的思路:
Boolean a= true;
While(a){
//展示表演菜单
Boolean b= true;
While(b){
//1、让用户输入选择
//2、写switch语句判断并输出展示信息,如果是1-5之间的数字,赋值b=false,判断actor是否为null,如果不为null,调用act方法。否则,输出提示信息,并再次展示表演菜单。
}
Boolean c= true
While(c){
//1提示用户是否继续
//2、让用户输入选择
//3、判断用户输入的数字,如果是0则return。如果是1 则c=flase。跳出该循环再次进入展示表演菜单的循环中其他提示用户输入有误
}
}如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星