【问题标题】:Java Nested If statement-Or Compared to !=Java 嵌套 If 语句 - 或与 != 相比
【发布时间】:2017-10-18 23:32:16
【问题描述】:

我不知道如何给这个问题起个名字,但基本上这是我的二十一点计划的一部分。另外,由于我不知道如何命名,我不知道如何查找它,这就是我在这里问的原因。所以我是说,当用户输入 1 或 11 作为 ace 值时,如果他们输入的不是 1 或 11,它会再次要求用户输入 1 或 11。在我的程序中,一切正常,除非用户输入 1,然后它只是再次提问。程序只应再次询问输入是否不等于 1 或 11。这是我的代码,因为我确保它始终为测试目的提供一个 ace:

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);

提前致谢。

【问题讨论】:

标签: java if-statement


【解决方案1】:

尝试使用 while 循环,而不是 If 语句。 while 循环可确保您的程序等待用户选择正确的答案。您的逻辑操作也犯了错误。在这种情况下使用“OR”的正确方法是使用“||”将用户输入分别与“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 循环永远不会停止!
  • 什么条件会满足您的while 循环?
  • 如果用户第一次做错但第二次做对了,你的答案会一直问这个问题。
  • 已修复。 while 循环中的条件应该是 AND 而不是 OR。
  • 谢谢,这次它工作正常。我会回去看看为什么和如何。
【解决方案2】:

您的代码存在一些问题,请考虑这个示例并与您的进行比较。

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);

}

【讨论】:

    【解决方案3】:

    表达式(1|11) 使用二进制OR,生成11

        11 = 01001
         1 = 00001
    (11|1) = 01001
    

    因此,比较与11!=player_ace_selection相同

    您应该更改代码以使用逻辑OR,即

    if (1!=player_ace_selection && 11!=player_ace_selection) {
        ...
    }
    

    此外,您需要修复card1 == "A"card1.equals("A") 的比较

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多