while循环select (5834.6 - MOD(5834.6,10)) + 10 from dual;
开始之前的语句没有被打印,循环中checkCudaErrors(cudaGraphicsGLRegisterBuffer(&_cgr, pbo_id,
cudaGraphicsRegisterFlagsNone));
的值没有从1开始打印。相反,它从一个随机的大int开始打印。
System.out.println("Value of i before loop = " + i);
答案 0 :(得分:3)
您正在直接比较导致无限循环的数组。这些结果正在印刷,但将成为吨和吨产量的顶部。修正你的比较。
答案 1 :(得分:0)
我明白了:
Value of i before loop = 0
2 2 1 ..................1
2 2 4 ..................2
...
建议您重建项目并重试。
正如最初发布的那样,您的代码不会终止,因为int[].equals(int[])
无法满足您的期望。
你可以试试这个。
private static boolean equals(int[] a, int[] b) {
if (a == null && b == null) {
// Both null
return true;
}
if (a == null || b == null) {
// One null
return false;
}
if (a.length != b.length) {
// Differ in length.
return false;
}
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
// Mismatch
return false;
}
}
// Same.
return true;
}
public void test() {
Random ran = new Random();
int[] in = {2, 5, 9};
int[] c_gen = new int[3];
int i = 0;
System.out.println("Value of i before loop = " + i);
while (!equals(c_gen, in)) {
c_gen[0] = ran.nextInt(10);
c_gen[1] = ran.nextInt(10);
c_gen[2] = ran.nextInt(10);
i++;
System.out.println(c_gen[0] + " " + c_gen[1] + " " + c_gen[2] + " .................." + i);
}
System.out.print("in = ");
for (int x : in) {
System.out.print(x + " ");
}
System.out.print("\n" + "c_gen = ");
for (int x : c_gen) {
System.out.print(x + " ");
}
System.out.println("\n" + "i = " + i);
}
答案 2 :(得分:0)
Sotirios的直觉是正确的 - 你的错误在while(!(c_gen.equals(in)))
行。你无法使用.equals(...)方法比较数组的相等性,因为“数组从Object继承它们的equals方法,[因此]将对内部数组执行身份比较,这将失败,因为a和b不要引用相同的数组。“ (source)。因此,c_gen
和in
将始终引用不同的数组(即使它们的内容相同),您的循环将永远存在。
public static void main(String[] args) {
Random ran = new Random();
int[] in = {2,5,9};
int[] c_gen = new int[3];
int i = 0;
System.out.println("Value of i before loop = " + i);
while(!Arrays.equals(in, c_gen)){
c_gen[0] = ran.nextInt(10);
c_gen[1] = ran.nextInt(10);
c_gen[2] = ran.nextInt(10);
i++;
System.out.println(c_gen[0] + " " + c_gen[1] + " " + c_gen[2] + " .................." + i);
}
System.out.print("in = ");
for(int x : in)
System.out.print(x + " ");
System.out.print("\n" + "c_gen = ");
for(int x : c_gen)
System.out.print(x + " ");
System.out.println("\n" + "i = " + i);
}
对我来说这是有效的(在有限的时间内终止),样本输出:
Value of i before loop = 0
1 9 9 ..................1
5 4 1 ..................2
1 1 6 ..................3
1 3 6 ..................4
.... //Omitted because of space
6 5 8 ..................1028
2 5 9 ..................1029
in = 2 5 9
c_gen = 2 5 9
i = 1029