所以代码是关于在do while中给出输入的限制.
在这种情况下,你有3个机会继续.在那之后do while停止,你也可以让oportunitie停止任何键加上输入,但是当你开始时,你是否有oportunitie(这里进入开关)退出程序.
问题或我遇到的问题就在这里.
在这种情况下,你有3个机会继续.在那之后do while停止,你也可以让oportunitie停止任何键加上输入,但是当你开始时,你是否有oportunitie(这里进入开关)退出程序.
问题或我遇到的问题就在这里.
也许这可能没有布尔,或者可能改变或添加我还不知道的东西.对不起,我试着找到一个答案,但我看到的只是关于出一个布局左右的循环.不是这样的.
Scanner kb = new Scanner(system.in); // c = continue char c; // attempt to limit the input to 3 times int attempt = 3; // option = op int op = 0; do { do{ System.out.println("Choose continue[0] or go out[1]."); while (!kb.hasNextInt()) { kb.nextLine(); System.out.println("It's not a number."); } op = kb.nextInt(); } while ( op <= -1 || op >= 2 ); switch (op) { case 0: System.out.println("Continue!"); break; case 1: //here I tried; attempt = -1 break; //is where I think it needs to be something default: break; } System.out.println("Do you wanna try again,"+attempt+" less?[c]+enter\n" + "Any other key + enter to exit."); c = kb.next(".").toupperCase().charat(0); attempt--; } while ( attempt > 0 && ( c == 'C' ) ); //here also to put !( op == 0 ) kb.close();
解决方法
如果用户选择0,您只需要求继续.
Scanner kb = new Scanner(system.in); // c = continue char c = 'a'; // attempt to limit the input to 3 times int attempt = 3; // option = op int op = 0; do { do{ System.out.println("Choose continue[0] or go out[1]."); while (!kb.hasNextInt()) { kb.nextLine(); System.out.println("It's not a number."); } op = kb.nextInt(); } while ( op <= -1 || op >= 2 ); switch (op) { case 0: System.out.println("Continue!"); System.out.println("Do you wanna try again,"+attempt+" less?[c]+enter\n" + "Any other key + enter to exit."); c = kb.next(".").toupperCase().charat(0); attempt--; break; case 1: attempt = -1; break; default: break; } } while ( attempt > 0 && ( c == 'C' ) ); kb.close();