我正在努力解决这个问题。我知道如何编写代码来确定数字是否介于1.0和10.0之间。假设noOfJudges是有效输入(在3和9之间)
for(noOfJudges = 0; noOfJudges < scores.length; noOfJudges++) {
scores[noOfJudges]=console.nextDouble();
while((scores[noOfJudges] < 1.0)||(scores[noOfJudges] > 10.0)) {
System.out.print("Please reenter the score (must be between 1.0 and 10.0, in .5 increments): ");
scores[noOfJudges] = console.nextDouble();
System.out.println();
}
System.out.println();
变量的有效输入将介于1.0和10.0之间,增量为0.5,即4.2不是有效输入,但4.5是。不知道如何继续...
答案 0 :(得分:2)
将输入乘以2并检查它是否在2.0和20.0之间,然后通过将其转换为int来截断小数位并检查截断值是否等于原始值(d == (double)(int)d
),或者绕过输入并查看它是否等于原始输入(d == Math.round(d)
)
答案 1 :(得分:0)
使用类似的东西
for(double i = 1; i < 10.0; i+=0.5) {
//...
}
请参阅链接for-loop, increment by double如何使用整数循环来防止被浮点运算的伪像咬住