我为此而苦苦挣扎。我知道如何编写代码以确定数字是否在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是有效输入。不确定如何继续进行...
最佳答案
将输入乘以2并检查它是否在2.0到20.0之间,然后通过将其转换为int来截断小数位,并检查截断后的值是否等于原始值(d == (double)(int)d
),或者舍入该输入以查看是否等于原始输入(d == Math.round(d)
)