Search code examples
integer-division

can not figure out how to keep my double...a double


if ((double) (points / tries) > hiScore) {
                        hiScore = (double) points / tries;
                        hiPoints = points;
                        hiTries = tries;

I cannot understand why hiScore, or even points / tries, always remain = 0 (points and tries are both ints, same as hiPoints and hiTries)


Solution

  • Try this

    if ( ((double) points / tries) > hiScore) {
                        hiScore = (double) points / tries;
                        hiPoints = points;
                        hiTries = tries;
    

    Or even this (shouldn't be necessary, because of the priority cast operation has over division):

    if ( (((double) points) / tries) > hiScore) {
                        hiScore = ((double) points) / tries;
                        hiPoints = points;
                        hiTries = tries;
    

    You need to cast your integer variable before division operation.