When given a pair of ints, I need my code to be able to return the int that is closest to 21 without going over [closed]

随声附和 提交于 2019-12-13 09:33:50

问题


// blackjack(7,17) -> 17  
// blackjack(21, 16) -> 21  
// blackjack(19,23) -> 19  

public int blackjack(int a, int b) {

}

After this I am really unsure of how to go about writing this code.


回答1:


Hi use this code.

if(a<=21 && b<=21){
    return Math.max(a, b);
}

This is a easy problem, try yourself next time.




回答2:


Maybe something like this ?

private int blackjack(int a, int b)
{
    int aa = 21-a >= 0 ? 21-a : 22;
    int bb = 21-b >= 0 ? 21-b : 22;

    if ( aa > 21 && bb > 21 )
    {
        return -1;
    }

    return aa < bb ? a : b;
}


来源:https://stackoverflow.com/questions/36848371/when-given-a-pair-of-ints-i-need-my-code-to-be-able-to-return-the-int-that-is-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!