Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:49:29+00:00 2026-06-16T17:49:29+00:00

My calculation is not working properly. I can not see anything wrong with the

  • 0

My calculation is not working properly. I can not see anything wrong with the code. Sometimes it does not calculate the score properly. Sometimes it does perfectly. I can not even understand when it does properly and when it does not do it properly.

Score calculation should be like this:

Ace can add to the total score either 1 or 11. If the score is higher than 21, then ace is calculated as 1; otherwise ace is 11.

Here is my code:

  // Updates the the value of the cards the player has in their hand
  int updateValueOfHand() {
    int result = 0;                             // it will be returned
    int ace = 0;                                // value of ace

    for (int i =0; i < playerHand.size(); i++)  // loop to see players hand
    {
      int cardValue;                            // card value of hand
      Card card=(Card)playerHand.get(i);        // check the card
      cardValue = card.getRank();
      if (cardValue == 1)                       // if card value is 1 (ace)
      {
        cardValue = 0;                         // assign to 0
        ace += 1;                              // ace is 1 (if there are 2 aces ace is 2 ...)
      }
      result = result + cardValue;             // result is card value (no ace)
    }
    //return result;
    println("Number of ace: " + ace);
    if (ace!=0)                                //if there is ace
    {
      for (int j=0; j<ace; j++)                // if there is more than 1 ace
      {
        if (result+11<=21) {                   // if result is <= 21 when you count ace as 11, then ace is 11
          result+=11;
        }        
        else {
          result+=1;                          // otherwise ace is 1
        }        
      }
    }
    return result;
  }

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:49:30+00:00Added an answer on June 16, 2026 at 5:49 pm

    Consider a hand with one King and two Aces. This should be calculated as 10 + 1 + 1, since otherwise it would be greater than 21.

    However, the program loops through each Ace, and:

    // if result is <= 21 when you count ace as 11, then ace is 11
    

    Since the King plus the first Ace counted as 11 is <= 21, the program chooses to count the first Ace as 11, but this in incorrect.

    Here is one idea to fix it: In your first for loop, increase result by 1 for every Ace, then in your second for loop, increase result by 10 for every Ace as long as it stays <= 21.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if the following code can cause redundant calculations, or is it
I'm using the jQuery calculation and have just about everything working properly, but I
i am working on a simple loan calculator, so simple that it's not even
I have a small javascript pricing calculation that is not working on a friend's
I wrote code below that is working perfectly for displaying the results of my
I'm trying to do a simple parse/calculation with some times, but dateJS does not
I cannot figure out why my calculation is not working. I am doing for
Another jquery calculation question. I've this, which is sample code from the plugin site
I'm working on a project that does some intense math calculations (arrays of matrices,
Is there a distance calculation implementation using hadoop map/reduce. I am trying to calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.