2015-05-21 213 views
0

while循环开始之前的语句System.out.println("Value of i before loop = " + i);未被打印,并且循环中的值i未从1开始打印。而是从随机大int开始打印。while()循环的问题

package main; 

import java.util.Random; 

public class Main { 
    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(!(c_gen.equals(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); 
    } 
} 
+1

我没有看到任何这样的行为在你的[代码](HTTP: //ideone.com/zDe7Ll)如你所描述的事实你有一个无限循环它意味着条件总是如此,所以你需要搜索如何比较数组 – silentprogrammer

回答

3

您直接比较导致无限循环的数组。这些结果正在印刷,但将成为吨和吨产量的顶部。修正你的比较。

+0

感谢的人,非常赞赏 – XRay

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); 
} 
+0

感谢的人,非常感谢 – XRay

0

Sotirios的直觉是正确的 - 你的错误是在while(!(c_gen.equals(in)))行。你不能使用.equals(...)方法来比较数组是否相等,因为“数组从Object继承它们的equals方法,[因此]将为内部数组执行标识比较,这将会失败,因为a和b不涉及相同的阵列。“ (source)。因此,因为c_genin将始终引用不同的数组(即使它们的内容相同),循环将永远存在。

尝试Arrays.equals(..)代替:

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 
+0

谢谢你,非常欣赏摊晒 – XRay