Guest User

Untitled

a guest
Jan 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. package magquad;
  2.  
  3. public class MagischesQuadrat {
  4.  
  5. /**
  6. * Daniel Martin
  7. * IT09
  8. * 15.04.2011
  9. */
  10.  
  11. private static int readInt(String string) {
  12. return 0;
  13. }
  14.  
  15.  
  16. public static void main(String[] args) {
  17.  
  18.  
  19.  
  20. System.out.println("Bitte eine Zahl eingeben");
  21. int n = readInt("");
  22.  
  23. while ((n < 2) | (n > 10)) {
  24. System.out.println("Bitte geben Sie eine Zahl größer als 2 und kleiner als 10 ein.");
  25. n = readInt("");
  26. }
  27.  
  28. int[][] quad = new int[n][n];
  29.  
  30. int zeile = n / 2;
  31. int spalte = n / 2 + 1;
  32. int m = 0;
  33.  
  34. for (m=1;m<n*n+1;m++){
  35.  
  36. quad[zeile][spalte] = m;
  37. zeile -= 1;
  38. spalte += 1;
  39.  
  40. if (zeile == -1) {
  41. zeile = n - 1;
  42. }
  43. if (spalte == n) {
  44. spalte = 0;
  45. }
  46. if (quad[spalte][zeile] > 0) {
  47. spalte++;
  48. zeile++;
  49.  
  50. if (zeile == n) {
  51.  
  52. zeile = 0;
  53. }
  54. if (spalte == n) {
  55. spalte = 0;
  56. }
  57. }
  58. }
  59.  
  60. int i;
  61. int j;
  62.  
  63. for (i = 0; i < n; i++) {
  64. for (j = 0; j < n; j++) {
  65. if (quad[i][j] < 10) {
  66. System.out.print(quad[i][j] + " |");
  67. } else {
  68. System.out.print(quad[i][j] + "|");
  69. }
  70. }
  71. System.out.println("");
  72.  
  73. }
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment