老师为什么我的是否继续观看那段程序,输入错了就不能重新输入了呢
来源:7-2 作业题
qq_ibertine_0
2020-07-02 09:33:09
俩个程序都是一样的上面的输入错了就可以,是否继续的就不可以
import java.util.Scanner;
public class AnimalsTest {
Scanner sc=new Scanner(System.in);
public void cc() {
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、小丑 ***************");
int n=sc.nextInt();
switch(n) {
case 1:
Bear one=new Bear("Bili",1);
one.act();
break;
case 2:
Lion two=new Lion("Lion",2,"灰色",'公');
two.act();
break;
case 3:
Monkey three=new Monkey("Tom",1,"金丝猴");
three.act();
break;
case 4:
Parrot four=new Parrot("ROse",1,"牡丹鹦鹉");
four.act();
break;
case 5:
Clown five=new Clown("Kolhe",5);
five.act();
break;
default:
System.out.println("error:输入错误,请重新输入");
break;
}
System.out.println("******是否继续观看(1/0)******");
int a=sc.nextInt();
switch (a){
case 0:
System.out.println("***** 欢迎下次光临 *****");
break;
case 1:
AnimalsTest wc=new AnimalsTest();
wc.cc();
break;
default:
System.out.println("error:输入错误,请重新输入");
break;
}
}
public static void main(String[] args) {
AnimalsTest wc=new AnimalsTest();
wc.cc();
}
}

1回答
同学你好,当是否继续观看输入0,1之外的数字时,会执行break;语句,这时cc()方法执行结束,主方法执行结束,所以就不能重新输入了。
同学可以使用while循环,当输入1时退出while循环。
参考代码如下:

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题
回答 2
回答 1