Vanilla_Fury

laba_5_3_java

Apr 11th, 2021 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     private static Scanner scConsole;
  9.  
  10.     private static final String MES_TASK = "Задание: построить квадрат нечётного порядка и визуализировать построение.\n";
  11.     private static final String MES_INPUT_REQUEST = "Введите размер квадрата:";
  12.     private static final String MES_TRY_AGAIN = "Повторите попытку: \n";
  13.     private static final String MES_FORMAT_INPUT_OF_PATH = "Пожалуйста, введите путь к файлу %s: ";
  14.     private static final String MES_ASK_OUTPUT_TO_FILE = "Хотите вывести ответ в файл?\n\t1 - да\n\t2 - нет\n";
  15.  
  16.     private static final String ERROR_FILE_CANNOT_BE_CREATED_OR_OPENED = "Файл не может быть создан или открыт. Повторите попытку:\n";
  17.     private static final String ERROR_CHOICE_IS_INCORRECT = "\tНадо ввести \"1\" или \"2\".\n\tПовторите попытку: ";
  18.     private static final String ERROR_NOT_NUMBER = "Введено не число.\n";
  19.     private static final String ERROR_FORMAT_NOT_EXPECTED_NUMBER = "Введено число, выходящее за рамки от %d до %d.\n";
  20.     private static final String ERROR_NOT_ODD_NUMBER = "Введено чётное число, а ожидалось нечётное.\n";
  21.  
  22.     private static final String SYS_OP_TO_FILE_YES = "Output to file";
  23.     private static final String SYS_OP_TO_FILE_NO = "Don't output to file";
  24.  
  25.     private static final Short MIN_SIZE = 3;
  26.     private static final Short MAX_SIZE = 5;
  27.  
  28.     private static String choose(String sChoice1, String sChoice2, String sQuestion) {
  29.         short nChoice = 2;
  30.         boolean bIsIncorrect;
  31.         String sAnswer;
  32.  
  33.         System.out.println(sQuestion + "\tВаш выбор: ");
  34.         do {
  35.             bIsIncorrect = false;
  36.             try {
  37.                 nChoice = Short.parseShort(scConsole.nextLine());
  38.             } catch (NumberFormatException e) {
  39.                 bIsIncorrect = true;
  40.             }
  41.             if (!bIsIncorrect && !(nChoice == 1) && !(nChoice == 2)) {
  42.                 bIsIncorrect = true;
  43.             }
  44.             if (bIsIncorrect) {
  45.                 System.out.println(ERROR_CHOICE_IS_INCORRECT);
  46.             }
  47.         } while (bIsIncorrect);
  48.  
  49.         sAnswer = nChoice == 1 ? sChoice1 : sChoice2;
  50.  
  51.         return sAnswer;
  52.     }
  53.  
  54.     private static String inputPathToFile(boolean bIsInput) {
  55.         String sPath;
  56.  
  57.         System.out.printf(MES_FORMAT_INPUT_OF_PATH, bIsInput ? "ввода" : "вывода");
  58.         sPath = scConsole.nextLine();
  59.         return sPath;
  60.     }
  61.  
  62.     private static int readNumberFromConsole() {
  63.         int nSize = 0;
  64.         boolean bIsIncorrect;
  65.  
  66.         System.out.println(MES_INPUT_REQUEST);
  67.         do {
  68.             bIsIncorrect = false;
  69.             try {
  70.                 nSize = Integer.parseInt(scConsole.nextLine());
  71.             } catch (NumberFormatException e) {
  72.                 bIsIncorrect = true;
  73.                 System.out.print(ERROR_NOT_NUMBER + MES_TRY_AGAIN);
  74.             }
  75.             if (!bIsIncorrect && (nSize < MIN_SIZE || nSize > MAX_SIZE)) {
  76.                 bIsIncorrect = true;
  77.                 System.out.printf(ERROR_FORMAT_NOT_EXPECTED_NUMBER + MES_TRY_AGAIN, MIN_SIZE, MAX_SIZE);
  78.             }
  79.             if (!bIsIncorrect && nSize % 2 == 0) {
  80.                 bIsIncorrect = true;
  81.                 System.out.print(ERROR_NOT_ODD_NUMBER + MES_TRY_AGAIN);
  82.             }
  83.         } while (bIsIncorrect);
  84.  
  85.         return nSize;
  86.     }
  87.  
  88.     public static int[][] makeMagicSquare(int n) {
  89.         int[][] aMatrix = new int[n][n];
  90.         int nHighMatrix = aMatrix.length - 1;
  91.         int i = 0;
  92.         int j = n / 2;
  93.         int nCount = 1;
  94.         int nWhenToStop = n * n + 1;
  95.  
  96.         do {
  97.             aMatrix[i][j] = nCount;
  98.  
  99.             i--;
  100.             j++;
  101.  
  102.             if (i < 0) i = nHighMatrix;
  103.             if (j > nHighMatrix) j = 0;
  104.  
  105.             if (aMatrix[i][j] != 0) {
  106.                 i += 2;
  107.                 j--;
  108.             }
  109.  
  110.             if (i > nHighMatrix) i -= aMatrix.length;
  111.             if (j < 0) j = nHighMatrix;
  112.  
  113.             System.out.println("Шаг " + nCount + ":");
  114.             for (int[] ints : aMatrix) {
  115.                 for (int k = 0; k < aMatrix.length; k++) {
  116.                     System.out.print("\t" + ints[k] + " ");
  117.                 }
  118.                 System.out.println();
  119.             }
  120.  
  121.             nCount++;
  122.         } while (nCount < nWhenToStop);
  123.  
  124.         return aMatrix;
  125.     }
  126.  
  127.     private static void outputToFile(int[][] aArr, String sPathToFile) {
  128.         boolean bOutputNotReady = true;
  129.         do {
  130.             try {
  131.                 PrintWriter fOutput = new PrintWriter(sPathToFile);
  132.                 fOutput.print("Размер квадрата: " + aArr.length + "x" + aArr.length);
  133.  
  134.                 for (int[] ints : aArr) {
  135.                     for (int j = 0; j < aArr.length; j++) {
  136.                         fOutput.print(ints[j] + " ");
  137.                     }
  138.                     fOutput.print("\n");
  139.                 }
  140.  
  141.                 fOutput.close();
  142.                 bOutputNotReady = false;
  143.             } catch (Exception e) {
  144.                 System.out.print(ERROR_FILE_CANNOT_BE_CREATED_OR_OPENED);
  145.                 String sShouldOutputInfoToFile = choose(SYS_OP_TO_FILE_YES, SYS_OP_TO_FILE_NO, MES_ASK_OUTPUT_TO_FILE);
  146.                 if (sShouldOutputInfoToFile.equals(SYS_OP_TO_FILE_YES)) {
  147.                     sPathToFile = inputPathToFile(false);
  148.                 } else {
  149.                     bOutputNotReady = false;
  150.                 }
  151.             }
  152.         } while (bOutputNotReady);
  153.     }
  154.  
  155.     private static void outputMatrix(int[][] aMagicSquare) {
  156.         System.out.print("Матрица:\n");
  157.         for (int[] ints : aMagicSquare) {
  158.             for (int j = 0; j < aMagicSquare.length; j++) {
  159.                 System.out.print("\t" + ints[j] + " ");
  160.             }
  161.             System.out.print("\n");
  162.         }
  163.  
  164.         String sShouldOutputInfoToFile = choose(SYS_OP_TO_FILE_YES, SYS_OP_TO_FILE_NO, MES_ASK_OUTPUT_TO_FILE);
  165.         if (sShouldOutputInfoToFile.equals(SYS_OP_TO_FILE_YES)) {
  166.             String sPathToFile = inputPathToFile(false);
  167.             outputToFile(aMagicSquare, sPathToFile);
  168.         }
  169.     }
  170.  
  171.     public static void main(String[] args) {
  172.         scConsole = new Scanner(System.in);
  173.  
  174.         // Путь к моему файлу вывода: /Users/sasha/Documents/___Университет/ОАиП/Пз/Лабы/блок 5/output.txt
  175.  
  176.         System.out.println(MES_TASK);
  177.         int nSize = readNumberFromConsole();
  178.         int[][] aMagicSquare = makeMagicSquare(nSize);
  179.         outputMatrix(aMagicSquare);
  180.         scConsole.close();
  181.     }
  182. }
  183.  
Add Comment
Please, Sign In to add comment