Java嵌套If语句或与之比较=
我不知道如何给这个问题命名,但基本上这是我21点计划的一部分。另外,由于我不知道如何命名这个,我不知道如何查找它,这就是为什么我在这里问。所以我的意思是,当用户输入1或11作为ace值时,如果他们输入的不是1或11,它会再次要求用户输入1或11。在我的程序中,除了当用户输入1时,一切都正常,然后它只是再次提问。程序只应再次询问输入值是否不等于1或11。以下是我的代码,因为我确保它始终为测试提供ace:Java嵌套If语句或与之比较=,java,if-statement,Java,If Statement,我不知道如何给这个问题命名,但基本上这是我21点计划的一部分。另外,由于我不知道如何命名这个,我不知道如何查找它,这就是为什么我在这里问。所以我的意思是,当用户输入1或11作为ace值时,如果他们输入的不是1或11,它会再次要求用户输入1或11。在我的程序中,除了当用户输入1时,一切都正常,然后它只是再次提问。程序只应再次询问输入值是否不等于1或11。以下是我的代码,因为我确保它始终为测试提供ace: String card1="A"; int total=0; Scanner input_va
String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1=="A"){
System.out.println("Do you want a 1 or 11 for the Ace?: ");
int player_ace_selection=input_var.nextInt();
if ((1|11)!=(player_ace_selection)){
System.out.println("Please enter a 1 or 11: ");
int new_selection=input_var.nextInt();
total=total + new_selection;
}
else {
total=total + player_ace_selection;
}
}
System.out.println(total);
提前谢谢 表达式(1 | 11)
使用二进制或
,产生11
:
11 = 01001
1 = 00001
(11|1) = 01001
因此,比较与11相同=玩家选择
您应该将代码更改为使用逻辑或
,即
if (1!=player_ace_selection && 11!=player_ace_selection) {
...
}
此外,您需要修复card1==“A”
比较card1.equals(“A”)
表达式(1 | 11)
使用二进制或
,从而生成11
:
11 = 01001
1 = 00001
(11|1) = 01001
因此,比较与11相同=玩家选择
您应该将代码更改为使用逻辑或
,即
if (1!=player_ace_selection && 11!=player_ace_selection) {
...
}
此外,您需要修复
card1==“A”
对card1.equals(“A”)
的比较,而不是If语句,请尝试while循环。while循环确保程序等待用户选择正确答案。您的逻辑操作也犯了错误。在此上下文中使用“或”的正确方法是使用“| |”分别将用户输入与“1”和“11”进行比较
String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1.equals("A")){
System.out.println("Do you want a 1 or 11 for the Ace?: ");
int player_ace_selection=input_var.nextInt();
while(player_ace_selection != 1 && player_ace_selection != 11){
System.out.println("Do you want a 1 or 11 for the Ace?: ");
player_ace_selection = input_var.nextInt();
}
total += player_ace_selection;
}
System.out.println(total);
请尝试while循环,而不是If语句。while循环确保程序等待用户选择正确答案。您的逻辑操作也犯了错误。在此上下文中使用“或”的正确方法是使用“| |”分别将用户输入与“1”和“11”进行比较
String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1.equals("A")){
System.out.println("Do you want a 1 or 11 for the Ace?: ");
int player_ace_selection=input_var.nextInt();
while(player_ace_selection != 1 && player_ace_selection != 11){
System.out.println("Do you want a 1 or 11 for the Ace?: ");
player_ace_selection = input_var.nextInt();
}
total += player_ace_selection;
}
System.out.println(total);
<>你的代码中有一些问题,请考虑这个例子并与你的比较。
String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1.equals("A")){ // compare the content not the reference (==)
System.out.println("Do you want a 1 or 11 for the Ace?: ");
try{ // wrap with try-catch block
int player_ace_selection = Integer.parseInt(input_var.nextLine()); //read the entire line and parse the input
if ((player_ace_selection!=1)&&(player_ace_selection!=11)){
System.out.println("Please enter a 1 or 11: ");
try{
int new_selection = Integer.parseInt(input_var.nextLine()); //again read the entire line and parse the input
total=total + new_selection;
}catch(NumberFormatException e){
// do something to catch the error
}
}
else {
total=total + player_ace_selection;
}
}catch(NumberFormatException e){
// do something to catch the error
}
System.out.println(total);
}
<>你的代码中有一些问题,请考虑这个例子并与你的比较。
String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1.equals("A")){ // compare the content not the reference (==)
System.out.println("Do you want a 1 or 11 for the Ace?: ");
try{ // wrap with try-catch block
int player_ace_selection = Integer.parseInt(input_var.nextLine()); //read the entire line and parse the input
if ((player_ace_selection!=1)&&(player_ace_selection!=11)){
System.out.println("Please enter a 1 or 11: ");
try{
int new_selection = Integer.parseInt(input_var.nextLine()); //again read the entire line and parse the input
total=total + new_selection;
}catch(NumberFormatException e){
// do something to catch the error
}
}
else {
total=total + player_ace_selection;
}
}catch(NumberFormatException e){
// do something to catch the error
}
System.out.println(total);
}
提示:
.nextInt()
vs.nextLine
->第一行不读取新行\n
没有缩写形式或,您正在1
和11
上按位或执行(1 | 11)=(玩家选择)
card1==“A”
不是这里唯一的问题,也不是主要的问题。投票重新打开。也可以使用.equals()
而不是=
来比较字符串,即card1
和“A”
查看并提示:.nextInt()
vs。nextLine
->第一行不读取新行\n
没有缩写或
,您正在1
和11
的(1 | 11)上执行按位或运算=(玩家选择)
card1==“A”
不是这里唯一的问题,也不是主要的问题。投票重新打开。也可以使用.equals()
而不是=
来比较字符串,即card1
和“A”
请参见,如果您的代码中有一个很大的错误,while
循环永远不会停止!什么条件可以满足您的while
循环?如果用户第一次出错但第二次正确,您的答案会不断询问这个问题。已修复。while循环中的条件应该是AND而不是OR。谢谢,这次它工作正常。我会回去看看原因和方法。你的代码中有一个很大的错误,而循环从未停止过!什么条件可以满足您的while
循环?如果用户第一次出错但第二次正确,您的答案会不断询问这个问题。已修复。while循环中的条件应该是AND而不是OR。谢谢,这次它工作正常。我会回去看看原因和方法。