我在计算机课上得到了这个看似简单的任务,但事实证明它比我更难:程序得到一个介于 0 - 1 之间的随机 float ,然后我需要将其转换为一个介于 0 - 17 之间的整数,不包括 0 & 17(十六个可能的值)。所以我开始制作一个简单的 for 循环,但它并没有真正起作用,所以我去硬编码它:
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;
}
}
但出于某种原因,我找不到它总是返回 16!谁能帮帮我? (请使用 JAVA)
最佳答案
提示 #1:1/16 是整数零。
提示 #2:如果将输入乘以 ??并将其转换为整数,你会得到什么? (不,您不需要一系列 if
语句....)
关于java - 在 0-1 到 int 0-17 之间 float 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32844882/