2015-05-21 176 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