2016-04-02 4 views
0

Ich hatte einmal in Mathe eine Sache namens computewall (in Deutsch: "Rechenwand", ich weiß nicht wie das auf Englisch heißt), du bekommst dort eine Pyramide und ein paar Ergebnisse. Es sieht z.B. wie folgt:Verhindere eine Endlosschleife mit rnd Werten

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

Da ich Urlaub habe, dachte ich: "Nun lass uns ein bisschen programmieren". Also habe ich versucht, ein Programm zu schreiben, das mir die eine Lösung berechnet, bei der die Zahlen 1-15 nur einmal in der Pyramide erscheinen.

Das Ergebnis einer Zahl, die eine Zeile zwischen zwei Zahlen steht, wird wie oben in der linken unteren Ecke berechnet. Es ist der absolute Wert der Substitution der darunter liegenden Zahlen.

Das Projekt, wo ich versuchte, dies auf Github und kann hier gefunden werden: https://github.com/SchoolGuy/Computewall

Das Problem ist, dass die do-while Schleife (in der Tat bei l.57 so etwas wie eine Endlosschleife jedes Mal, wenn ich das Programm ausführen verursacht Es ist einfach endlos zu versuchen, die Argumente anzupassen). Meine Frage ist, wie ich dieses Problem loswerden kann!

P.S .: Ich habe viel auskommentiert, um den genauen Punkt zu finden, also habe keine Angst vor der Größe.

bearbeiten 2016.02.04 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; 
} 
} 

Dies ist die komplette Klasse.

+3

Könnten Sie bitte den Code in Ihre Frage einfügen? – user2004685

+0

Es kann unter github –

+2

nur den relevanten Teil des Codes in Ihrer Frage geschrieben werden. Niemand möchte nach GitHub gehen und den Code sehen. – Blip

Antwort

1

Es gibt einige große Probleme mit diesem Code, aber ich werde Beantworten Sie Ihre spezifische Frage zur While-Schleife. Hier ist der Code:

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

Beachten Sie, dass, wenn entweder duplicateCheck oder ohCheck wird auf true beim ersten Durchlauf dieses do-while, dann sind Sie in einer Endlosschleife stecken. Sie kommen als false in die Schleife, können aber im ersten Durchgang der Schleife auf true gesetzt werden. Es gibt keine Bedingung in der Schleife, die einen von beiden auf "Falsch" setzen würde, und somit wäre die Bedingung (duplicateCheck | ohCheck) immer erfüllt.

+0

Und der Rest der Probleme? Ich habe das Programm im Hintergrund laufen, wird es jemals das Ergebnis geben? –

+0

Mit 'uniqueCheck' machen Sie etwas sehr ähnliches. Also bezweifle ich, dass es enden wird. Es sollte nicht länger als ein oder zwei Sekunden dauern, bis es ausgeführt wird. Wenn es länger dauert, sind Sie wahrscheinlich in einer Endlosschleife. – mwm314

+0

Thx, ich werde suchen.Könnten Sie bitte auch suchen Ich bin ein echter Neuling :( –

0

Wenn eine der Prüfungen (duplicateCheck | ohCheck) war immer wahr ist, dann wurde die var nie auf false gesetzt, so da hatte ich meine Endlosschleife