老师,我把这个作为方法放上面怎么会报错呢?sti这个方法

老师,我把这个作为方法放上面怎么会报错呢?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();
//				}
			
				
			
		}
		
		
	}

}


正在回答

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

2回答

同学你好,break必须在switch或循环之内。否则会提示错误

https://img1.sycdn.imooc.com//climg/6221e0b40947398306860287.jpg

这里同学可以尝试使用return代替

祝学习愉快~

  • 热爱编程学习 提问者 #1

    老师,那我怎么把这段重复代码,弄出来做一个方法用了呢?

    2022-03-04 21:17:49
  • 同学你好,

    1、没有理解同学要表达的意思,同学是想将ti()简写吗?还是if..else if 中不重复调用sti()方法呢?建议同学描述具体~

    2、建议同学将完整代码进行反馈,上述只有测试类,老师无法进行修改完善~

    祝学习愉快~


    2022-03-05 10:05:05
  • 老师,测试类得这个代码重复得写,我想写一个方法把这些代码放进去。请问怎么写呢?

    https://img1.sycdn.imooc.com//climg/62230824092e8c8b09950542.jpg

    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() {
    
    		System.out.println("\n******(0/1)是否继续观看**********\n");
    		
    			
    	}
    //	public void stic() {
    //		Scanner sc=new Scanner(System.in);
    //		int my;
    //		System.out.println("\n******(0/1)是否继续观看**********\n");
    //		my=sc.nextInt();
    //		if(my!=1)
    //			System.out.println("成功退出");
    //			
    //	}
    	
    	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();
    			try {
    				n=sc.nextInt();	
    			}catch(java.util.InputMismatchException e) {
    				System.out.println("输入错误");
    				sc.next();
    				continue;
    			}
    				
    			//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();
    				mi=sc.nextInt();
    				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();
    				mi=sc.nextInt();
    				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();
    				mi=sc.nextInt();
    				if(mi!=1)
    					System.out.println("成功退出");
    					break;
    			}else if(n==5) {
    				Clown c=new Clown("脚才高跷,进行杂技魔术表扬","Kanle",5);
    				c.act();
    				c.skill();
    				test.sti();
    				mi=sc.nextInt();
    				if(mi!=1)
    					System.out.println("成功退出");
    					break;
    			}else {
    				System.out.println("输入错误");
    			}
    			
    				
    			
    		}
    		
    		
    	}
    	
    }


    2022-03-05 14:50:18
好帮手慕小脸 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 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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