2016-04-02 119 views
0

我曾經在數學中有一個名爲computewall(德語:「Rechenwand」,我不知道這是如何稱爲英語),你會得到一個金字塔和一些結果。它看起來像像這樣:防止與rnd值無限循環

0 
    0|0 
    0|0|0 
4|0|0|0 
5|9|0|0|0 

因爲我有假期,我想:「那麼讓我們編碼一下」。所以我試着編寫一個程序來計算我唯一在金字塔中出現的數字1-15的解決方案。

計算兩個數字之間的一行數字的結果,就像您可以在左上角看到的一樣。它是下面數字替換的絕對值。

,我想這是在Github上,並可以在這裏找到的項目:https://github.com/SchoolGuy/Computewall

的問題是,我每次運行程序時的do-while循環在1.57會導致像一個死循環(實際上是隻是無盡的努力去適應論據)。我的問題是我如何擺脫這個問題!

P.S .:我評論了很多以找到確切點,所以不要害怕大小。

編輯2016年4月2日15:50:

package Main; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Random; 

public class computewall { 

int[][] computewallInt = null; 
int count; 
ArrayList<Integer> sortedComputewall = null; 
Random rnd = new Random(); 

public computewall() { 
    count = 0; 
    sortedComputewall = new ArrayList<>(); 
    computewallInt = new int[5][5]; 
    computewallInt[0][0] = 0; 
    computewallInt[1][0] = 0; 
    computewallInt[1][1] = 0; 
    computewallInt[2][0] = 0; 
    computewallInt[2][1] = 0; 
    computewallInt[2][2] = 0; 
    computewallInt[3][0] = 0; 
    computewallInt[3][1] = 0; 
    computewallInt[3][2] = 0; 
    computewallInt[3][3] = 0; 
    computewallInt[4][0] = 0; 
    computewallInt[4][1] = 0; 
    computewallInt[4][2] = 0; 
    computewallInt[4][3] = 0; 
    computewallInt[4][4] = 0; 
    calculate(); 
    System.out.println("Rounds till solved: " + count); 
    System.out.println("  " + computewallInt[0][0]); 
    System.out.println("  " + computewallInt[1][0] + " | " + computewallInt[1][1]); 
    System.out.println(" " + computewallInt[2][0] + " | " + computewallInt[2][1] + " | " + computewallInt[2][2]); 
    System.out.println(" " + computewallInt[3][0] + " | " + computewallInt[3][1] + " | " + computewallInt[3][2] + " | " + computewallInt[3][3] + " | "); 
    System.out.println(computewallInt[4][0] + " | " + computewallInt[4][1] + " | " + computewallInt[4][2] + " | " + computewallInt[4][3] + " | " + computewallInt[4][4]); 
} 

public void calculate() { 
    boolean uniqueCheck = false; 
    ArrayList<String> usedPosibilities = new ArrayList<>(); 
    System.out.println("-------------------------"); 
    do { 
     count++; 
     sortedComputewall.clear(); 
     boolean duplicateCheck = false; 
     boolean ohCheck = false; 
     //Set values and search for duplicates and 0 
     do { 
      computewallValueReplace(computewallInt, 4, 0, rnd.nextInt(16)); 
      computewallValueReplace(computewallInt, 4, 1, rnd.nextInt(16)); 
      computewallValueReplace(computewallInt, 4, 2, rnd.nextInt(16)); 
      computewallValueReplace(computewallInt, 4, 3, rnd.nextInt(16)); 
      computewallValueReplace(computewallInt, 4, 4, rnd.nextInt(16)); 
      ///* 
      if (computewallInt[4][0] == computewallInt[4][1] | computewallInt[4][0] == computewallInt[4][2] | computewallInt[4][0] == computewallInt[4][3] | computewallInt[4][0] == computewallInt[4][4] 
        | computewallInt[4][1] == computewallInt[4][2] | computewallInt[4][1] == computewallInt[4][3] | computewallInt[4][1] == computewallInt[4][4] 
        | computewallInt[4][2] == computewallInt[4][3] | computewallInt[4][2] == computewallInt[4][4] | computewallInt[4][3] == computewallInt[4][4]) { 
       duplicateCheck = true; 
      } 
      if (computewallInt[4][0] == 0 | (computewallInt[4][1] == 0) | (computewallInt[4][1] == 0) | (computewallInt[4][3] == 0) | computewallInt[4][4] == 0) { 
       ohCheck = true; 
      } 
     } while (duplicateCheck | ohCheck); 
     usedPosibilities.add(String.valueOf(computewallInt [4][0]) + String.valueOf(computewallInt [4][1]) + String.valueOf(computewallInt [4][2]) 
       + String.valueOf(computewallInt [4][3]) + String.valueOf(computewallInt [4][4])); 
     System.out.println("Lowest row calculated"); 

     //Rest of calculating 
     computewallInt[3][0] = Math.abs(computewallInt[4][0] - computewallInt[4][1]); 
     computewallInt[3][1] = Math.abs(computewallInt[4][1] - computewallInt[4][2]); 
     computewallInt[3][2] = Math.abs(computewallInt[4][2] - computewallInt[4][3]); 
     computewallInt[3][3] = Math.abs(computewallInt[4][3] - computewallInt[4][4]); 
     computewallInt[2][0] = Math.abs(computewallInt[3][0] - computewallInt[3][1]); 
     computewallInt[2][1] = Math.abs(computewallInt[3][1] - computewallInt[3][2]); 
     computewallInt[2][2] = Math.abs(computewallInt[3][2] - computewallInt[3][3]); 
     computewallInt[1][0] = Math.abs(computewallInt[2][0] - computewallInt[2][1]); 
     computewallInt[1][1] = Math.abs(computewallInt[2][1] - computewallInt[2][2]); 
     computewallInt[0][0] = Math.abs(computewallInt[1][0] - computewallInt[1][1]); 
     System.out.println("Rest of table calculated"); 
     sortedComputewall.add(computewallInt[0][0]); 
     sortedComputewall.add(computewallInt[1][0]); 
     sortedComputewall.add(computewallInt[1][1]); 
     sortedComputewall.add(computewallInt[2][0]); 
     sortedComputewall.add(computewallInt[2][1]); 
     sortedComputewall.add(computewallInt[2][2]); 
     sortedComputewall.add(computewallInt[3][0]); 
     sortedComputewall.add(computewallInt[3][1]); 
     sortedComputewall.add(computewallInt[3][2]); 
     sortedComputewall.add(computewallInt[3][3]); 
     sortedComputewall.add(computewallInt[3][3]); 
     sortedComputewall.add(computewallInt[4][4]); 
     sortedComputewall.add(computewallInt[4][1]); 
     sortedComputewall.add(computewallInt[4][2]); 
     sortedComputewall.add(computewallInt[4][3]); 
     Object[] sortedComputeWallInt = sortedComputewall.toArray(); 
     Arrays.sort(sortedComputeWallInt); 
     if (sortedComputewall.contains(0) | !sortedComputewall.contains(1) | !sortedComputewall.contains(2) | !sortedComputewall.contains(3) | !sortedComputewall.contains(4) | 
       !sortedComputewall.contains(5) | !sortedComputewall.contains(6) | !sortedComputewall.contains(7) | !sortedComputewall.contains(8) | !sortedComputewall.contains(9) | 
       !sortedComputewall.contains(10) | !sortedComputewall.contains(11) | !sortedComputewall.contains(12) | !sortedComputewall.contains(13) | !sortedComputewall.contains(14) | 
       !sortedComputewall.contains(15)) { 
      uniqueCheck = true; 
     } 
     System.out.println("-------------------------"); 
    } while (uniqueCheck); 
} 

public void computewallValueReplace(int[][] array, int row, int position, int value) { 
    array[row][position] = value; 
} 
} 

這是完整的類。

+3

您能否在您的問題中包括代碼? – user2004685

+0

它可以在github上查看 –

+2

只發布代碼中相關部分的問題。沒有人願意去github上看看代碼。 – Blip

回答

1

沒有與此代碼中的一些大問題,但我會回答關於while循環的具體問題。這裏是你的代碼:

do { 
     computewallValueReplace(computewallInt, 4, 0, rnd.nextInt(16)); 
     computewallValueReplace(computewallInt, 4, 1, rnd.nextInt(16)); 
     computewallValueReplace(computewallInt, 4, 2, rnd.nextInt(16)); 
     computewallValueReplace(computewallInt, 4, 3, rnd.nextInt(16)); 
     computewallValueReplace(computewallInt, 4, 4, rnd.nextInt(16)); 
     ///* 
     if (computewallInt[4][0] == computewallInt[4][1] | computewallInt[4][0] == computewallInt[4][2] | computewallInt[4][0] == computewallInt[4][3] | computewallInt[4][0] == computewallInt[4][4] 
       | computewallInt[4][1] == computewallInt[4][2] | computewallInt[4][1] == computewallInt[4][3] | computewallInt[4][1] == computewallInt[4][4] 
       | computewallInt[4][2] == computewallInt[4][3] | computewallInt[4][2] == computewallInt[4][4] | computewallInt[4][3] == computewallInt[4][4]) { 
      duplicateCheck = true; 
     } 
     if (computewallInt[4][0] == 0 | (computewallInt[4][1] == 0) | (computewallInt[4][1] == 0) | (computewallInt[4][3] == 0) | computewallInt[4][4] == 0) { 
      ohCheck = true; 
     } 
    } while (duplicateCheck | ohCheck); 

注意,如果只將duplicateCheckohCheck設置爲true這一做,而第一關,那麼你就停留在一個無限循環。它們作爲false進入循環,但可以在循環的第一遍中設置爲true。循環中沒有任何條件會將它們中的任何一個設置爲false,因此總是會滿足條件(duplicateCheck | ohCheck)

+0

還有其他問題?我有程序在後臺運行,它會給出結果嗎? –

+0

你使用'uniqueCheck'做了一些非常相似的事情。所以我懷疑它會結束。執行時間不應超過一秒鐘,如果花費更長時間,您可能會陷入無限循環。 – mwm314

+0

Thx,我會搜索。你能否也請搜索我是一個真正的新手:( –

0

當檢查之一(duplicateCheck | ohCheck)是有史以來真,則VAR從未設置爲false,所以我有我的死循環