Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float between 0-1 to int 0-17 issue

Tags:

java

i got this seemingly simple task in computer class that has proven itself to be harder than i though: the program gets a random float between 0 - 1 that then i need to turn into an int between 0 - 17 not including 0 & 17 (sixteen possible values). so i started by making a simple for loop which didn't really work so i went and hard coded it:

public static float step(float input){
    if(input < (1/16 * 1)){
        return 1;
    }else if(input < (1/16 * 2)){
        return 2;
    }else if(input < (1/16 * 3)){
        return 3;
    }else if(input < (1/16 * 4)){
        return 4;
    }else if(input < (1/16 * 5)){
        return 5;
    }else if(input < (1/16 * 6)){
        return 6;
    }else if(input < (1/16 * 7)){
        return 7;
    }else if(input < (1/16 * 8)){
        return 8;
    }else if(input < (1/16 * 9)){
        return 9;
    }else if(input < (1/16 * 10)){
        return 10;
    }else if(input < (1/16 * 11)){
        return 11;
    }else if(input < (1/16 * 12)){
        return 12;
    }else if(input < (1/16 * 13)){
        return 13;
    }else if(input < (1/16 * 14)){
        return 14;
    }else if(input < (1/16 * 15)){
        return 15;
    }else{
        return 16;
    }
}

but for some reason that i can just not find it returns always 16! could anyone help me? (JAVA please)

like image 261
FolkLoricEar Avatar asked Dec 02 '22 14:12

FolkLoricEar


1 Answers

Hint #1: 1/16 is the integer zero.

Hint #2: If you multiply input by ?? and convert it to an integer, what do you get? ( No, you don't need a chain of if statements .... )

like image 197
Stephen C Avatar answered Dec 13 '22 14:12

Stephen C