【发布时间】:2015-02-25 08:50:26
【问题描述】:
我是 Java 编程新手,我正在尝试学习其中的技巧。我遇到了一个问题,问题源于我无法检查变量是否为整数。
int[] values = new int[3];
private Random rand = new Random();
num1 = rand.nextInt(70);
num2 = rand.nextInt(70);
num3 = rand.nextInt(70);
values[1] = num1 + num2
//we are dividing values[1] by num3 in order to produce an integer solution. For example (4 + 3) / 2 will render an integer answer, but (4 + 3) / 15 will not.
//The (correct) answer is values[2]
values[2] = values[1]/num3
if (num3 > values[1]) {
//This is done to ensure that we are able to divide by the number to get a solution (although this would break if values[1] was 1).
num3 = 2
while (values[2] does not contain an integer value) {
increase num3 by 1
work out new value of values[2]
}
} else {
while (values[2] does not contain an integer value) {
increase num3 by 1
work out new value of values[2] <--- I'm not sure if this step is necessary
}
}
System.out.println(values[2]);
【问题讨论】:
-
if (num == (int)num) { // 数字是整数 }
-
values的定义位置和方式? -
这毫无意义。基元数组需要一个类型。他们只能“持有”这种类型。所以如果你有一个
int[],所有的元素都是整数。
标签: java loops while-loop logic division