老师,我把这个作为方法放上面怎么会报错呢?sti这个方法
package com.imooc.xiangmuzuoye; import java.util.Scanner; public class AnimalTest { public void st() { System.out.println("********1、棕熊********"); System.out.println("********2、狮子********"); System.out.println("********3、猴子********"); System.out.println("********4、鹦鹉********"); System.out.println("********5、小丑********"); } public void sti() { Scanner sc=new Scanner(System.in); int my; System.out.println("\n******(0/1)是否继续观看**********\n"); my=sc.nextInt(); if(my!=1) System.out.println("成功退出"); break; } public static void main(String[] args) { // TODO Auto-generated method stub AnimalTest test=new AnimalTest(); System.out.println("********欢迎来到太阳马戏团********"); int n; int mi; Scanner sc=new Scanner(System.in); while(true) { System.out.println("********请选择表演者********"); test.st(); n=sc.nextInt(); if(n==0) { System.out.println("成功退出"); break; }else if(n==1){ Bear a=new Bear("Bill",18); a.love(); a.act(); a.skill(); test.sti(); mi=sc.nextInt(); if(mi!=1) System.out.println("成功退出"); break; }else if(n==2) { Lion L=new Lion("Lain",2,"灰色",'公'); L.love(); L.act(); L.skill(); test.sti(); if(mi!=1) System.out.println("成功退出"); break; }else if(n==3) { Monkey m=new Monkey("Tom",1,"金丝猴"); m.love(); m.act(); m.skill(); test.sti(); if(mi!=1) System.out.println("成功退出"); break; }else if(n==4) { Parrot p=new Parrot("Rose",1,"牡丹鹦鹉"); p.love(); p.act(); p.skill(); test.sti(); }else if(n==5) { Clown c=new Clown("脚才高跷,进行杂技魔术表扬","Kanle",5); c.act(); c.skill(); test.sti(); }else { System.out.println("输入错误"); } // switch(n) { // case 1: // Bear a=new Bear("Bill",18); // a.love(); // a.act(); // a.skill(); // test.sti(); // // case 2: // Lion L=new Lion("Lain",2,"灰色",'公'); // L.love(); // L.act(); // L.skill(); // test.sti(); // case 3: // Monkey m=new Monkey("Tom",1,"金丝猴"); // m.love(); // m.act(); // m.skill(); // test.sti(); // case 4: // Parrot p=new Parrot("Rose",1,"牡丹鹦鹉"); // p.love(); // p.act(); // p.skill(); // test.sti(); // case 5: // Clown c=new Clown("脚才高跷,进行杂技魔术表扬","Kanle",5); // c.act(); // c.skill(); // test.sti(); // } } } }
14
收起
正在回答
2回答
同学你好,break必须在switch或循环之内。否则会提示错误
这里同学可以尝试使用return代替
祝学习愉快~
好帮手慕小脸
2022-03-05 17:48:00
同学你好,关于"提示是否观看表演"这部分内容,同学可以使用while循环实现此功能,并将if..ifelse...else修改为switch..case,修改后代码如下所示:
public class AnimalTest { public void st() { 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 static void main(String[] args) { AnimalTest at = new AnimalTest(); int chias, a1; Scanner sc = new Scanner(System.in); while (true) { at.st(); chias = sc.nextInt(); switch (chias) { case 1: Bear bear = new Bear("Bill", 1); bear.act(); break; case 2: Lion lain = new Lion("Lain", 2, "公狮", "灰色"); lain.act(); break; case 3: Monkey tom = new Monkey("Tom", 1, "金丝猴"); tom.act(); break; case 4: Parrot rose = new Parrot("Rose", 1, "牡丹鹦鹉"); rose.act(); break; case 5: Clown kahle = new Clown("Kahle", 5); kahle.act(); break; default: System.out.println("请选择1-5"); break; } while (true) { System.out.println("**********是否继续观看(1/0)**********"); System.out.println("*******请输入对应数字进行操作:*******"); try { a1 = sc.nextInt(); } catch (java.util.InputMismatchException e) { System.out.println("*******输入数据格式有误,不能有非数字!*******"); sc.next(); continue; } if (a1 == 0) { System.out.println("*******欢迎下次光临*******"); System.exit(0); } else if (a1 == 1) break; else { System.out.println("*******输入数据格式有误,请输入1或0*******"); continue; } } } } }
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星