1

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.

5
  • Hint: .nextInt() vs .nextLine -> the first doesn't read the new line \n Commented May 19, 2017 at 0:57
  • There is no short form OR, you are doing a bitwise or on 1 and 11 at (1|11)!=(player_ace_selection) Commented May 19, 2017 at 0:57
  • card1 == "A" is not the only problem here, and it's not the main one. Voting to reopen. Commented May 19, 2017 at 0:58
  • Also use .equals() instead of == for comparing Strings i.e. card1 and "A" Commented May 19, 2017 at 0:58
  • See best way to format multiple 'or' conditions in an if statement (Java) and how do I compare strings in Java? Commented May 19, 2017 at 1:12

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

7 Comments

You have a great error in your code, the while loop never stops!
What condition would satisfy your while loop?
Your answer keeps asking the question if user gets it wrong the first time but gets it right the second time.
Fixed it. The condition in the while loop should have been an AND instead of OR.
Thanks, this time it worked correctly. I will go back and look at why and hows.
|
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.