PALADIN 458S
PALADIN 458S

Reputation: 25

Program Not Calculating Properly Given All Variables

[Still Learning...] While trying to create a program to calculate the advantage/disadvantage of a blackjack table given all rule variations, this calculator only displays the initial input from the user for the number of decks. All of the other rules from the user seem to be completely ignored when it is run. For example, with 6-decks, the result is always -0.54% regardless of whatever you enter for the rest of the rules. It only takes into account the number of decks. How do I resolve this issue?

I have been looking through my two books (Java 24 Hours, and Java 21 Days) and to me (about a month into this), it looks like it should be working.

The code is here:

{ public static void main(String[] args){   double edge = 0;

    Scanner sc = new Scanner(System.in);
    System.out.println("How many decks (1, 2, 4, 6, 8)");
    int decks = sc.nextInt();
    System.out.println("Dealer hits Soft 17 (Y or N)");
    String soft17 = sc.next();
    System.out.println("Double after splits (Y or N)");
    String doubleAfterSplit = sc.next();
    System.out.println("Double 10/11 only (Y or N)");
    String double1011 = sc.next();
    System.out.println("Double 9/10/11 only (Y or N)");
    String double91011 = sc.next();
    System.out.println("Resplit Aces (Y or N)");
    String rSA = sc.next();
    System.out.println("Late Surrender (Y or N)");
    String lateSurrender = sc.next();
    System.out.println("Early Surrender (Y or N)");
    String earlySurrender = sc.next();
    System.out.println("Lose all doubles/splits vs. Natural (Y or N)");
    String loseAllDS = sc.next();
    sc.close();

    //Number of Decks Option
    if (decks == 1) {
        edge += 0.01;
    } else if (decks == 2) {
        edge -= 0.32;
    } else if (decks == 4) {
        edge -= 0.49;
    } else if (decks == 6) {
        edge -= 0.54;
    } else if (decks == 8) {
        edge -= 0.57;
    } else {
        System.out.println("You made a mistake.  Start over.");
    }

    //Soft 17 Options
    if (soft17 == "Y") {
        edge -= 0.20;
    } else {
        edge += 0;
    }

    //Double After Splits Option
    if (doubleAfterSplit == "Y") {
        edge += 0.14;
    } else {
        edge += 0;
    }

    //Double 10 and 11 Only Option
    if (double1011 == "Y") {
        edge -= 0.17;
    } else {
        edge += 0;
    }

    //Double 9, 10, and 11 Only Option
    if (double91011 == "Y") {
        edge -= 0.08;
    } else {
        edge += 0;
    }

    //Resplit Aces Option
    if (rSA == "Y") {
        edge += 0.08;
    } else {
        edge += 0;
    }

    //Late Surrender Option
    if (lateSurrender == "Y") {
        edge += 0.08;
    } else {
        edge += 0;
    }

    //Early Surrender Option
    if (earlySurrender =="Y") {
        edge += 0.65;
    } else {
        edge += 0;
    }

    //Lose on All Doubles and Splits vs Natural Option
    if (loseAllDS == "Y") {
        edge -= 0.11;
    } else {
        edge += 0;
    }

    if (edge >= 0) {
        System.out.println("Your advantage is: " + edge + "%");
    } else {
        System.out.println("Your disadgantage is: " + edge + "%");
    }


}

Upvotes: 0

Views: 38

Answers (3)

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

The problem is the use of == instead of the equals() method.

== checks if two variables refer to the same object, whereas equals() checks if the value of the two objects are equal.

Sometimes == will appear to work with String objects because there is an object pool that reuses the same String object for literals (ie Strings specified as constant quoted values), so the same object is actually being referenced by both Strings.

However, this is not always the case, for example, when collecting input from the console for which new String objects are allocated.

In general, you should use an object's equals() method to check value equality between objects.

Upvotes: 1

Elemental
Elemental

Reputation: 7491

Your basic problem is very simple: In general the == operator doesn't work on strings in the way that you want. == in Java check if the two objects are the same object NOT if they have the same value.

So your lines to check for 'Y' answers don't do what you think they do; instead you should be using the equals method like so:

 if (double91011.equals("Y"))

Upvotes: 1

ZqBany
ZqBany

Reputation: 211

Change soft17 == "Y" to soft17.equals("Y") etc.

Read this question

Upvotes: 1

Related Questions