2016-04-02 22 views
0

Bir keresinde matematikte computewall diye bir şey vardı (Almancada: "Rechenwand", bunun nasıl İngilizce olarak adlandırıldığını bilmiyorum), bir piramit ve birkaç sonuç elde edersiniz. Ör. Bunun gibi:Rnd değerleriyle sonsuz bir döngüyü engelle

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

Çünkü tatillerim var, diye düşündüm: "Hadi biraz kod yapalım". Bu yüzden, 1-15 sayılarının piramidde sadece bir kez göründüğü tek çözümü hesaplayan bir program yazmaya çalıştım.

İki sayı arasındaki bir satırın üstündeki bir sayı sonucu, sol alt köşede görebileceğiniz gibi hesaplanır. Alttaki sayıların yerine geçmesinin mutlak değeridir.

Bu Github ve burada bulunabilir çalıştı projesi: https://github.com/SchoolGuy/Computewall

sorun l.57 de do-while döngü ben program çalıştırmak her zaman aslında bir sonsuz döngü gibi bir şey (neden olmasıdır argümanlara uymaya çalışmak sadece sonsuzdur. Sorum şu: bu problemden nasıl kurtulabilirim!

P.S .: Kesin noktayı bulmak için çok fazla yorum yaptım, bu yüzden boyuttan korkmayın.

Düzenleme 02.04.2016 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; 
} 
} 

Bu tam sınıftır. çeklerin

+3

Sorunuzdaki kodu da ekleyebilir misiniz? – user2004685

+0

Github –

+2

adresinden yalnızca sorgunuzdaki kodun ilgili kısmını gönderebilirsiniz. Kimse github'a gitmek ve kodu görmek istemez. – Blip

cevap

1

Orada bu kodla bazı büyük sorunlar vardır, ama olacak while döngüsü ile ilgili özel sorunuzu yanıtlayın.

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); 

duplicateCheck veya bu do-while ilk geçişte true ayarlanırsa ohCheck ya, o zaman sonsuz döngüye girdi olduğunuzu gözlemleyin: İşte kodudur. Döngüye false olarak gelirler, ancak döngünün ilk geçişinde true olarak ayarlanabilirler. Döngüde, bunların hiçbirini yanlış olmayacak hiçbir koşul yoktur ve bu nedenle (duplicateCheck | ohCheck) koşulu her zaman tatmin olur.

+0

Ve diğer sorunlar? Arka planda çalışan program var, sonuç verecek mi? –

+0

'uniqueCheck' ile gerçekten benzer bir şey yapıyorsunuz. Yani bitireceğinden şüpheliyim. Bunu yapmak için bir ya da iki saniyeden fazla sürmemeliyiz ... eğer daha uzun sürüyorsa, muhtemelen sonsuz bir döngüde olursunuz. – mwm314

+0

Thx, arayacağım.Ben de arama yapabilir misiniz? ( –

0

zaman bir (duplicateCheck | ohCheck) bu yüzden orada benim sonsuz döngüye vardı, sonra var false olarak ayarlanır asla hiç doğruydu