求教各路大神~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package com.imooc.test; import com.imooc.model.Bear; import com.imooc.model.Lion; import com.imooc.model.Monkey; import com.imooc.model.Parrot; import com.imooc.model.Clown; import java.util.InputMismatchException; import java.util.Scanner; public class CircusTest { /** * 提示信息 */ public void displayMenu() { 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 void displayMenu2() { System.out.println( "**********是否继续观看(1/0)**********" ); Scanner sc = new Scanner(System.in); int input = sc.nextInt(); if (input== 1 ) { this .displayMenu(); } else if (input== 0 ) { System.out.println( "**********是否继续观看(1/0)**********" ); } else { System.out.println( "*******输入信息不正确,请重新输入**********" ); } } /** * 主方法用switch结构 */ public static void main(String[] args) { CircusTest test = new CircusTest(); Scanner sc = new Scanner(System.in); int input = 0 ; test.displayMenu(); while ( true ) { try { input = sc.nextInt(); } catch (InputMismatchException e) { System.out.println( "*******输入信息不正确,请重新输入**********" ); sc.next(); continue ; } // 若数据超出0-5的范围,提示错误信息并且要求重新输入! if ((input < 0 ) | (input > 5 )) { System.out.println( "*******输入的数字超出范围,请重新输入*******" ); continue ; } if (input == 0 ) { System.out.println( "**********欢迎下次光临**********" ); break ; } switch (input) { case 1 : Bear bear = new Bear( "Bill" , 1 ); bear.act(); System.out.println(); test.displayMenu2(); break ; case 2 : Lion lion = new Lion( "Lain" , 2 , "灰色" , "公狮" ); lion.act(); System.out.println(); test.displayMenu2(); break ; case 3 : Monkey monkey = new Monkey( "Tom" , 1 , "金丝猴" ); monkey.act(); System.out.println(); test.displayMenu2(); break ; case 4 : Parrot parrot = new Parrot( "Rose" , 1 , "牡丹鹦鹉" ); parrot.act(); System.out.println(); test.displayMenu2(); break ; case 5 : Clown clown = new Clown( "Kahle" , 5 ); clown.act(); System.out.println(); test.displayMenu2(); break ; } } } } 想请问大神们,我怎么才能把程序在运行到 switch 结构里,然后输出某一位表演者的act()以后,能够显示“是否继续观看( 1 / 0 )”的内容,然后再输入 1 来继续 switch 的input,然后输入 0 直接结束整个程序啊?我现在写的程序直接输入 0 的话,其实并没有结束,还要再输入一次 0 才结束整个程序~求教~先谢谢大神们了~! |
正在回答
你现在是在default里直接continue了,IAct actor = null写在while外面还是里面都无所谓。如果输错了,直接进入下一次循环,与actor值无关。这种已经是最优解。
如果default里不写continue:
第一种
while(true)
IAct actor = null //循环开始actor赋值为null
switch
case: actor=值;break;
default: //这里什么都不用写,直接给提示就可以
if (actor != null) actor.act();
else continue;
询问循环
第二种
IAct actor = null
while(true) //循环开始actor没有重新为null,第二次表演就可能actor已经指向实例对象
switch
case: actor=实例对象;break;
default: actor=null; //输入错误时再赋值为null
if (actor != null) actor.act();
else continue;
询问循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 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 CircusTest3 { /** * 提示信息 */ public void displayMenu() { 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 void displayMenu2() { Scanner sc1 = new Scanner(System.in); while ( true ) { System.out.println( "**********是否继续观看(1/0)**********" ); String input1 = sc1.next(); if (input1.equals( "1" )) { // 结束这个询问是否继续表演的循环,进入到下一次表演的循环 displayMenu(); break ; } else if (input1.equals( "0" )) { // 程序直接安全退出 System.out.println( "**********欢迎下次光临**********" ); System.exit( 0 ); } else { // 给提示,继续下一次询问是否继续 System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } } } public static void main(String[] args) { CircusTest3 test = new CircusTest3(); test.displayMenu(); Scanner sc = new Scanner(System.in); String input = null ; IAct actor = null ; while ( true ) { input = sc.next(); switch (input) { // 根据输入的指令调取表演者信息 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 ; default : System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } actor.act(); test.displayMenu2(); } } } 感谢大神们指教~! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 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 CircusTest3 { /** * 提示信息 */ public void displayMenu() { 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 void displayMenu2() { Scanner sc1 = new Scanner(System.in); while ( true ) { System.out.println( "**********是否继续观看(1/0)**********" ); String input1 = sc1.next(); if (input1.equals( "1" )) { // 结束这个询问是否继续表演的循环,进入到下一次表演的循环 displayMenu(); break ; } else if (input1.equals( "0" )) { // 程序直接安全退出 System.out.println( "**********欢迎下次光临**********" ); System.exit( 0 ); } else { // 给提示,继续下一次询问是否继续 System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } } } public static void main(String[] args) { CircusTest3 test = new CircusTest3(); test.displayMenu(); Scanner sc = new Scanner(System.in); String input = null ; while ( true ) { IAct actor = null ; try { input = sc.next(); } catch (Exception ex) { System.out.println( "*******输入信息不正确,请重新输入**********" ); sc.next(); continue ; } switch (input) { // 根据输入的指令调取表演者信息 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 ; default : System.out.println( "*******输入信息不正确,请重新输入**********" ); break ; } actor.act(); test.displayMenu2(); } } } 老师,这样子写,我一开始输入字母程序就出错了,显示NullPointerException。。。应该怎么修改啊? |
同学你好,同学的代码整体完成的不错,但是还可以精简一下,比如
1、在switch外定义接口的引用为null,在case后,指向具体的实现类对象,并且不需要将接口的引用强制转换为子类对象。
2、在switch后调用act()和displayMenu2()方法。
3、建议使用switch-case,不使用if-else。
代码如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | public class CircusTest2 { /** * 提示信息 */ public void displayMenu() { 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 void displayMenu2() { Scanner sc1 = new Scanner(System.in); while ( true ) { System.out.println( "**********是否继续观看(1/0)**********" ); String input1 = sc1.next(); if (input1.equals( "1" )) { // 结束这个询问是否继续表演的循环,进入到下一次表演的循环 displayMenu(); break ; } else if (input1.equals( "0" )) { // 程序直接安全退出 System.out.println( "**********欢迎下次光临**********" ); System.exit( 0 ); } else { // 给提示,继续下一次询问是否继续 System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } } } public static void main(String[] args) { CircusTest2 test = new CircusTest2(); test.displayMenu(); Scanner sc = new Scanner(System.in); while ( true ) { IAct actor = null ; String input = sc.next(); switch (input) { // 根据输入的指令调取表演者信息 case "1" : actor = new Bear( "bile" , 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 ; default : System.out.println( "请输入0-5之间的数" ); break ; } actor.act(); test.displayMenu2(); } } } |
祝:学习愉快~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | package com.imooc.test; import java.util.InputMismatchException; 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 CircusTest2 { /** * 提示信息 */ public void displayMenu() { 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 void displayMenu2() { Scanner sc1 = new Scanner(System.in); while ( true ) { System.out.println( "**********是否继续观看(1/0)**********" ); String input1 = sc1.next(); if (input1.equals( "1" )) { // 结束这个询问是否继续表演的循环,进入到下一次表演的循环 displayMenu(); break ; } else if (input1.equals( "0" )) { // 程序直接安全退出 System.out.println( "**********欢迎下次光临**********" ); System.exit( 0 ); } else { // 给提示,继续下一次询问是否继续 System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } } } public static void main(String[] args) { CircusTest2 test = new CircusTest2(); test.displayMenu(); Scanner sc = new Scanner(System.in); String input = sc.next(); while ( true ) { if (input.equals( "0" )) { System.out.println( "**********欢迎下次光临**********" ); break ; } else if ((input.equals( "1" )) || (input.equals( "2" )) || (input.equals( "3" )) || (input.equals( "4" )) || (input.equals( "5" ))) { switch (input) { // 根据输入的指令调取表演者信息 case "1" : IAct bear = new Bear( "Bill" , 1 ); ((Bear) bear).act(); System.out.println(); test.displayMenu2(); break ; case "2" : IAct lion = new Lion( "Lain" , 2 , "灰色" , "公狮" ); ((Lion) lion).act(); System.out.println(); test.displayMenu2(); break ; case "3" : IAct monkey = new Monkey( "Tom" , 1 , "金丝猴" ); ((Monkey) monkey).act(); System.out.println(); test.displayMenu2(); break ; case "4" : IAct parrot = new Parrot( "Rose" , 1 , "牡丹鹦鹉" ); ((Parrot) parrot).act(); System.out.println(); test.displayMenu2(); break ; case "5" : IAct clown = new Clown( "Kahle" , 5 ); ((Clown) clown).act(); System.out.println(); test.displayMenu2(); break ; } input = sc.next(); } else { System.out.println( "*******输入信息不正确,请重新输入**********" ); input = sc.next(); continue ; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package com.imooc.test; import com.imooc.model.Animal; import com.imooc.model.Bear; import com.imooc.model.Lion; import com.imooc.model.Monkey; import com.imooc.model.Parrot; import com.imooc.model.Clown; import com.imooc.model.IAct; import java.util.InputMismatchException; import java.util.Scanner; public class CircusTest { /** * 提示信息 */ public void displayMenu() { 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、小丑 **********" ); } /** * 主方法用switch结构 */ public static void main(String[] args) { CircusTest test = new CircusTest(); Scanner sc = new Scanner(System.in); int input = 0 ; test.displayMenu(); while ( true ) { try { input = sc.nextInt(); } catch (InputMismatchException e) { System.out.println( "*******输入信息不正确,请重新输入**********" ); sc.next(); continue ; } // 若数据超出0-5的范围,提示错误信息并且要求重新输入! if ((input < 0 ) | (input > 5 )) { System.out.println( "*******输入的数字超出范围,请重新输入*******" ); continue ; } if (input == 0 ) { System.out.println( "**********欢迎下次光临**********" ); break ; } switch (input) { // 根据输入的指令调取表演者信息 case 1 : IAct bear = new Bear( "Bill" , 1 ); ((Bear) bear).act(); System.out.println(); break ; case 2 : IAct lion = new Lion( "Lain" , 2 , "灰色" , "公狮" ); ((Lion) lion).act(); System.out.println(); break ; case 3 : IAct monkey = new Monkey( "Tom" , 1 , "金丝猴" ); ((Monkey) monkey).act(); System.out.println(); break ; case 4 : IAct parrot = new Parrot( "Rose" , 1 , "牡丹鹦鹉" ); ((Parrot) parrot).act(); System.out.println(); break ; case 5 : IAct clown = new Clown( "Kahle" , 5 ); ((Clown) clown).act(); System.out.println(); break ; } while ( true ) { // 是否继续表演的循环 System.out.println( "**********是否继续观看(1/0)**********" ); Scanner sc1 = new Scanner(System.in); int input1 = 0 ; try { input1 = sc.nextInt(); } catch (InputMismatchException e) { System.out.println( "*******输入信息不正确,请重新输入**********" ); sc.next(); continue ; } if (input1 == 1 ) { // 结束这个询问是否继续表演的循环,进入到下一次表演的循环 test.displayMenu(); break ; } else if (input1 == 0 ) { // 程序直接安全退出 System.out.println( "**********欢迎下次光临**********" ); System.exit( 0 ); } else { // 给提示,继续下一次询问是否继续 System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package com.imooc.test; import com.imooc.model.Bear; import com.imooc.model.Lion; import com.imooc.model.Monkey; import com.imooc.model.Parrot; import com.imooc.model.Clown; import java.util.InputMismatchException; import java.util.Scanner; public class CircusTest { /** * 提示信息 */ public void displayMenu() { 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、小丑 **********" ); } /** * 主方法用switch结构 */ public static void main(String[] args) { CircusTest test = new CircusTest(); Scanner sc = new Scanner(System.in); int input = 0 ; test.displayMenu(); while ( true ) { try { input = sc.nextInt(); } catch (InputMismatchException e) { System.out.println( "*******输入信息不正确,请重新输入**********" ); sc.next(); continue ; } // 若数据超出0-5的范围,提示错误信息并且要求重新输入! if ((input < 0 ) | (input > 5 )) { System.out.println( "*******输入的数字超出范围,请重新输入*******" ); continue ; } if (input == 0 ) { System.out.println( "**********欢迎下次光临**********" ); break ; } switch (input) { // 根据输入的指令调取表演者信息 case 1 : Bear bear = new Bear( "Bill" , 1 ); bear.act(); System.out.println(); break ; case 2 : Lion lion = new Lion( "Lain" , 2 , "灰色" , "公狮" ); lion.act(); System.out.println(); break ; case 3 : Monkey monkey = new Monkey( "Tom" , 1 , "金丝猴" ); monkey.act(); System.out.println(); break ; case 4 : Parrot parrot = new Parrot( "Rose" , 1 , "牡丹鹦鹉" ); parrot.act(); System.out.println(); break ; case 5 : Clown clown = new Clown( "Kahle" , 5 ); clown.act(); System.out.println(); break ; } while ( true ) { // 是否继续表演的循环 System.out.println( "**********是否继续观看(1/0)**********" ); Scanner sc1 = new Scanner(System.in); int input1 = 0 ; try { input1 = sc.nextInt(); } catch (InputMismatchException e) { System.out.println( "*******输入信息不正确,请重新输入**********" ); sc.next(); continue ; } if (input1 == 1 ) { //结束这个询问是否继续表演的循环,进入到下一次表演的循环 test.displayMenu(); break ; } else if (input1 == 0 ) { //程序直接安全退出 System.out.println( "**********欢迎下次光临**********" ); System.exit( 0 ); } else { //给提示,继续下一次询问是否继续 System.out.println( "*******输入信息不正确,请重新输入**********" ); continue ; } } } } } |
你这个程序还有一个问题,就是问你是否继续表演时,如果选择不是0或者1,比如3,提示错误,但是再选择1,就是开始表演了,而不是表演菜单选择。
分成两个部分,“表演”为一个部分,“是否继续”为一个部分。
给你个缩进结构的思路,自己品一下。
while(true)
//输入表演菜单
switch //表演的switch
case:表演内容;break;
while(true) //是否继续表演的循环
if 1,break; //结束这个询问是否继续表演的循环,进入到下一次表演的循环
else if 0,System.exit(0); //这个表示程序直接安全退出,不管你有几重循环
else //输错情况,只给提示,继续下一次询问是否继续
- 参与学习 人
- 提交作业 9404 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧