从一个开关中传出一段时间,但没有布尔值,它';有可能吗?JAVA
因此,代码是关于在一段时间内给出输入的限制。 在这种情况下,您有3个机会继续。在do while停止之后,您也有机会在按下任何键并输入之前停止,但在启动时,您也有机会(此处输入开关)退出程序。 问题或我被困的地方就在这里 这可能不需要布尔值,也可能改变或添加一些我还不知道的东西。对不起,我试图找到一个答案,但我所看到的只是一个带有布尔值的while循环。不是这样的从一个开关中传出一段时间,但没有布尔值,它';有可能吗?JAVA,java,switch-statement,do-while,Java,Switch Statement,Do While,因此,代码是关于在一段时间内给出输入的限制。 在这种情况下,您有3个机会继续。在do while停止之后,您也有机会在按下任何键并输入之前停止,但在启动时,您也有机会(此处输入开关)退出程序。 问题或我被困的地方就在这里 这可能不需要布尔值,也可能改变或添加一些我还不知道的东西。对不起,我试图找到一个答案,但我所看到的只是一个带有布尔值的while循环。不是这样的 Scanner kb = new Scanner(System.in); // c = continue c
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();
Scanner kb=新的扫描仪(System.in);
//c=继续
字符c;
//尝试将输入限制为3次
int尝试=3;
//选项=op
int op=0;
做{
做{
System.out.println(“选择继续[0]或退出[1]”;
而(!kb.hasnetint()){
kb.nextLine();
System.out.println(“这不是一个数字”);
}
op=kb.nextInt();
}而(op=2);
开关(op){
案例0:
System.out.println(“继续!”);
打破
案例1://我尝试了;尝试=-1
break;//我认为它应该是
违约:
打破
}
System.out.println(“您想再试一次吗,+trust+”更少?[c]+enter\n”
+“任何其他键+进入退出。”);
c=kb.next(“.”).toUpperCase().charAt(0);
企图--;
}而(尝试>0&(c=='c');
//这里也要放!(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();
Scanner kb=新的扫描仪(System.in);
//c=继续
字符c='a';
//尝试将输入限制为3次
int尝试=3;
//选项=op
int op=0;
做{
做{
System.out.println(“选择继续[0]或退出[1]”;
而(!kb.hasnetint()){
kb.nextLine();
System.out.println(“这不是一个数字”);
}
op=kb.nextInt();
}而(op=2);
开关(op){
案例0:
System.out.println(“继续!”);
System.out.println(“您想再试一次吗,+trust+”更少?[c]+enter\n”
+“任何其他键+进入退出。”);
c=kb.next(“.”).toUpperCase().charAt(0);
企图--;
打破
案例1:
尝试=-1;
打破
违约:
打破
}
}而(尝试>0&(c=='c');
kb.close();
这个问题有点难理解,但我想你想要的是
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;
the_loop:
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 the_loop; //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();
Scanner kb=新的扫描仪(System.in);
//c=继续
字符c;
//尝试将输入限制为3次
int尝试=3;
//选项=op
int op=0;
_循环:
做{
做{
System.out.println(“选择继续[0]或退出[1]”;
而(!kb.hasnetint()){
kb.nextLine();
System.out.println(“这不是一个数字”);
}
op=kb.nextInt();
}而(op=2);
开关(op){
案例0:
System.out.println(“继续!”);
打破
案例1://我尝试了;尝试=-1
打破_循环;//这是我认为它需要的东西
违约:
打破
}
System.out.println(“您想再试一次吗,+trust+”更少?[c]+enter\n”
+“任何其他键+进入退出。”);
c=kb.next(“.”).toUpperCase().charAt(0);
企图--;
}而(尝试>0&(c=='c');
//这里也要放!(op==0)
kb.close();
注意
循环
和中断循环代码>我认为您可以通过以下方法轻松完成此操作,而无需使用case开关:
static Scanner kb = new Scanner(System.in);
public static void main(String args[]){
if(getContinueOption(3)){
//do what you want to do
}
}
private static boolean getContinueOption(int attempts) {
System.out.println("Would you like to continue? Y[1] : N[0]");
while(attempts > 0){
int input = kb.nextInt();
if(input == 1){
return true;
}
attempts--;
System.out.println( (attempts == 0)? "Ok :(" : "Are you sure?");
}
return false;
}
它实际上比这更简单,并且避免了使用if
语句,尽管我不确定在现实生活中为什么会这样做,但这是一个很好的练习,可以复习概念
让我们先看一下实现:
import java.util.*;
import java.lang.*;
class SillyEnforcer {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
char c = 'C';
int attempt = 3;
int op = 0;
while(attempt > 0 && c == 'C') { // attempt loop
do {
System.out.println("Enter 0 to continue, 1 to exit");
while (!kb.hasNextInt()) {
kb.nextLine();
System.out.println("Not a number, try again.");
}
op = kb.nextInt();
switch(op) {
case 0:
continue;
case 1:
op = -1;
break;
default:
System.out.println("Number can only be 0 or 1.");
op = 0; // positional trickery
continue;
}
} while(op != -1);
System.out.println("Do you wanna try again, ("+ attempt + " attempt" +
((attempt > 1) ? "s" : "") + " left) ? C<ENTER> to continue\n"
+ "Any other key<ENTER> to exit");
c = kb.next(".").toUpperCase().charAt(0);
attempt = attempt - ((c == 'C') ? 1 : 0);
}
}
}
import java.util.*;
导入java.lang.*;
类愚蠢的执行者{
公共静态void main(字符串[]args){
扫描仪kb=新扫描仪(System.in);
字符c='c';
int尝试=3;
int op=0;
而(尝试>0&&c=='c'){//尝试循环
做{
System.out.println(“输入0继续,输入1退出”);
而(!kb.hasnetint()){
kb.nextLine();
System.out.println(“不是数字,再试一次。”);
}
op=kb.nextInt();
开关(op){
案例0:
继续;
案例1:
op=-1;
打破
违约:
System.out.println(“数字只能是0或1”);
op=0;//位置欺骗
继续;
}
}而(op!=-1);
System.out.println(“您想再试一次吗,(“+trunt+”trunt)”+
((尝试>1)?“s”:“)+”左)?C继续\n
+“任何其他退出密钥”);
c=kb.next(“.”).toUpperCase().charAt(0);
尝试=尝试-((c=='c')?1:0);
}
}
}
内在魔法
注意,通过对案例0
使用continue
并对案例1
重新分配op=-1
,我们可以正确管理消息,并且通过在默认值:
中分配op=0
我们可以处理边缘情况,即聪明的人将-1
作为上面的整数输入
注意,我们