Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package magquad;
- public class MagischesQuadrat {
- /**
- * Daniel Martin
- * IT09
- * 15.04.2011
- */
- private static int readInt(String string) {
- return 0;
- }
- public static void main(String[] args) {
- System.out.println("Bitte eine Zahl eingeben");
- int n = readInt("");
- while ((n < 2) | (n > 10)) {
- System.out.println("Bitte geben Sie eine Zahl größer als 2 und kleiner als 10 ein.");
- n = readInt("");
- }
- int[][] quad = new int[n][n];
- int zeile = n / 2;
- int spalte = n / 2 + 1;
- int m = 0;
- for (m=1;m<n*n+1;m++){
- quad[zeile][spalte] = m;
- zeile -= 1;
- spalte += 1;
- if (zeile == -1) {
- zeile = n - 1;
- }
- if (spalte == n) {
- spalte = 0;
- }
- if (quad[spalte][zeile] > 0) {
- spalte++;
- zeile++;
- if (zeile == n) {
- zeile = 0;
- }
- if (spalte == n) {
- spalte = 0;
- }
- }
- }
- int i;
- int j;
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++) {
- if (quad[i][j] < 10) {
- System.out.print(quad[i][j] + " |");
- } else {
- System.out.print(quad[i][j] + "|");
- }
- }
- System.out.println("");
- }
- }
- }
Add Comment
Please, Sign In to add comment