Bob G.
Bob G.

Reputation: 143

Java Nested If statement-Or Compared to !=

I wasn't sure how to give a title for this problem, but basically this is part of my blackjack program. Also, since I did not know how to title this, I wasn't sure how to look it up, which is why I am asking here. So I am saying that when the user enters either 1 or 11 for the ace value, if they enter something other than 1 or 11, it asks the user again to put in 1 or 11. In my program everything works fine except when the user enters 1, then it just asks the question again. The program should only asks again if the input is not equal to 1 or 11. Here is my code as I made sure it always gives an ace for testing purposes:

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

Thanks in advance.

Upvotes: 1

Views: 108

Answers (3)

Rahul Chowdhury
Rahul Chowdhury

Reputation: 681

Instead of an If statement, try a while loop. A while loop ensures that your program waits for your user to pick the right answer. You also made a mistake with your logical operations. The correct way to use "OR" in this context is to compare your user input to both '1' and '11' separately using '||'.

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

Upvotes: 1

Yahya
Yahya

Reputation: 14102

There are some problems in your code, please consider this example and compare it with yours.

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

}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727087

The expression (1|11) uses binary OR, which produces 11:

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

Hence, the comparison is the same as 11!=player_ace_selection

You should change the code to use logical OR, i.e.

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

In addition, you need to fix card1 == "A" comparison for card1.equals("A")

Upvotes: 3

Related Questions