Java > Logic-2 > blackjack (CodingBat Solution)

Problem:

Given 2 int values greater than 0, return whichever value is nearest to 21 without going over. Return 0 if they both go over.

blackjack(19, 21) → 21
blackjack(21, 19) → 21
blackjack(19, 22) → 19


Solution:

public int blackjack(int a, int b) {
  if (a > 21 && b > 21) {
      return 0;
  }else if (a > 21) {
      return b;
  } else if (b > 21) {
      return a;
  }
  
  int sumA = 21 - a;
  int sumB = 21 - b;
  
  if (sumA > sumB) {
      return b;
  } else {
      return a;
  }
  
}


22 comments :

  1. if(a>21 && b>21) return 0;
    return (a>21)? b : (b>21)? a : Math.max(a,b);

    ReplyDelete
    Replies
    1. @Kico
      Good solution. I didn't work those small tasks which are good for logic and thinking and now i have very bad and big solutions. You are in good form ;)

      Delete
  2. Can somebody explain how this works
    return (a>21)? b : (b>21)? a : Math.max(a,b);

    ?

    ReplyDelete
    Replies
    1. if (a > 21) --> b
      if (a <= 21 && b > 21) --> a
      if (a <= 21 && b =< 21) --> Math.max(a,b)

      Delete
    2. return (a>21)? b : (b>21)? a : Math.max(a,b);

      how do u write this in python?
      im doing all java tasks in python cuz ive done all python tasks

      Delete
  3. if (a > 21 && b > 21)
    return 0;
    if (Math.abs(21 - a) < Math.abs(21 -b) && a <= 21 || b > 21)
    return a;
    return b;

    ReplyDelete
  4. public int blackjack(int a, int b) {
    /* if (a > 21 && b > 21) {return 0;}
    if (a > 21 || b==21) {return b;}
    if (b > 21 || a==21) {return a;}
    return (a < 21 && b < 21 && a > b)?a:b;
    */


    //optimize
    if (a > 21 && b > 21) {return 0;}
    return (a>21)?b:(b>21)?a:Math.max(a,b);

    }

    ReplyDelete
  5. public int blackjack(int a, int b) {
    /* if (a > 21 && b > 21) {return 0;}
    if (a > 21 || b==21) {return b;}
    if (b > 21 || a==21) {return a;}
    return (a < 21 && b < 21 && a > b)?a:b;
    */


    //optimize
    if (a > 21 && b > 21) {return 0;}
    return (a>21)?b:(b>21)?a:Math.max(a,b);

    //explaining return (a>21)?b:(b>21)?a:Math.max(a,b);
    // is '(a>21) ?' if yes than 'b' else':' check '(b>21)?' if yes than 'a' else':' get max of (a,b) using math.max function.

    }

    ReplyDelete
  6. Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. 넷마블 먹튀

    ReplyDelete
  7. The most effective means bring in some form of e-book and enjoy this almost from anywhere is during the entire fascinating process involving mp 3 mp3 audio books. The true sound e-book outlets that always one thinks of as rapidly as. บาคาร่า

    ReplyDelete
  8. Are the Odds against you in this Online Casino Game? To read the latest game reviews Visit bintang9.com/bm/nova88-maxbet.aspx Unarguably Internet is the buzz word today, be it shopping, searching for information, or casino gambling.

    ReplyDelete
  9. A debt of gratitude is in order for offering this quality data to us. I truly delighted in perusing. Will without a doubt going to impart this URL to my companions.  dg casino

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Also, the player has so many other options to take advantage of. The player can Double Down, Split Pairs, Take Insurance, and even Surrender a bad hand in some casinos. dig this

    ReplyDelete
  12. public int blackjack (int a, int b) {

    if(rtr(a)==0&&rtr(b)==0)

    return 0;

    else if(21-rtr(a) <21-rtr(b))

    return a;

    else

    return b;

    }

    public int rtr(int a)

    if(a>21)

    return 0;

    else return a;

    }

    ReplyDelete
  13. public int blackjack(int a, int b) {
    if (a > 21 && b > 21){
    return 0;
    }

    if (a <= 21){
    if (b > 21){
    return a;
    }
    if (a > b){
    return a;
    }
    }
    return b;
    }

    ReplyDelete
  14. dapatkan keuntungan ratusan juta setiap hari dengan bergabung bersama situs resmi terpercaya SARANAPELANGI

    ReplyDelete
  15. Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on. casino relevant Backlinks

    ReplyDelete
  16. if (a<=21 && b<=21) {
    if (a < b) {
    return b;
    }
    if (a>b){
    return a;
    }

    }
    if (a>21 && b<=21){
    return b;

    }
    if (a<=21 && b>21){
    return a;
    }
    else
    return 0;

    ReplyDelete
  17. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. สล็อตjokerฟรีเครดิต

    ReplyDelete
  18. return a >21 && b> 21 ? 0 : a >= b && a<=21 || b > 21 ? a : b;

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact