从do内部的开关出来,但没有布尔值,这可能吗? Java的
发布时间:2020-12-15 04:41:03 所属栏目:Java 来源:网络整理
导读:所以代码是关于在do while中给出输入的限制. 在这种情况下,你有3个机会继续.在那之后do while停止,你也可以让oportunitie停止任何键加上输入,但是当你开始时,你是否有oportunitie(这里进入开关)退出程序. 问题或我遇到的问题就在这里. 也许这可能没有布尔,或
|
所以代码是关于在do while中给出输入的限制.
在这种情况下,你有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]+entern"
+ "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]+entern"
+ "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();
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
