import java.util.*;public class Algorithmus{ private int weite = 0; private int menge = 0; private int[][] zahlenfeld =null; public Algorithmus() { weite = 5; menge = weite*weite; zahlenfeld = new int[weite][weite]; ausrechnen(); ausgabe(); } public Algorithmus(int a) { weite = a; menge = weite*weite; zahlenfeld = new int[weite][weite]; ausrechnen(); //ausgabe(); } public void ausrechnen() { //Startposition im Feld Mitte des oberen Feldes int posx=((weite-1)/2), posy = 0; zahlenfeld[posy][posx]=1; for(int x=2;x<menge+1;x++) { if (posy<1) { if (posx<1) { posy=weite-1; posx=weite-1; } else { posy=weite-1; posx=posx-1; } } else { if (posx<1) { posx=weite-1; posy=posy-1; } else { posx=posx-1; posy=posy-1; } } if (zahlenfeld[posy][posx]!=0) { if (posy==(weite-1)) { if (posx==(weite-1)) { posy=1; posx=0; } else { posy=1; posx=posx-1; } } else { posy=posy+2; posx=posx+1; } } zahlenfeld[posy][posx]=x; } } public void ausgabe() { for (int x=0; x<weite; x++) { System.out.println(); for (int y=0; y<weite; y++) { System.out.print(zahlenfeld[x][y]+" "); } } } public static void main(String[] args) { Algorithmus neu = new Algorithmus(); } }