Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Main {
- private static Scanner scConsole;
- private static final String MES_TASK = "Задание: построить квадрат нечётного порядка и визуализировать построение.\n";
- private static final String MES_INPUT_REQUEST = "Введите размер квадрата:";
- private static final String MES_TRY_AGAIN = "Повторите попытку: \n";
- private static final String MES_FORMAT_INPUT_OF_PATH = "Пожалуйста, введите путь к файлу %s: ";
- private static final String MES_ASK_OUTPUT_TO_FILE = "Хотите вывести ответ в файл?\n\t1 - да\n\t2 - нет\n";
- private static final String ERROR_FILE_CANNOT_BE_CREATED_OR_OPENED = "Файл не может быть создан или открыт. Повторите попытку:\n";
- private static final String ERROR_CHOICE_IS_INCORRECT = "\tНадо ввести \"1\" или \"2\".\n\tПовторите попытку: ";
- private static final String ERROR_NOT_NUMBER = "Введено не число.\n";
- private static final String ERROR_FORMAT_NOT_EXPECTED_NUMBER = "Введено число, выходящее за рамки от %d до %d.\n";
- private static final String ERROR_NOT_ODD_NUMBER = "Введено чётное число, а ожидалось нечётное.\n";
- private static final String SYS_OP_TO_FILE_YES = "Output to file";
- private static final String SYS_OP_TO_FILE_NO = "Don't output to file";
- private static final Short MIN_SIZE = 3;
- private static final Short MAX_SIZE = 5;
- private static String choose(String sChoice1, String sChoice2, String sQuestion) {
- short nChoice = 2;
- boolean bIsIncorrect;
- String sAnswer;
- System.out.println(sQuestion + "\tВаш выбор: ");
- do {
- bIsIncorrect = false;
- try {
- nChoice = Short.parseShort(scConsole.nextLine());
- } catch (NumberFormatException e) {
- bIsIncorrect = true;
- }
- if (!bIsIncorrect && !(nChoice == 1) && !(nChoice == 2)) {
- bIsIncorrect = true;
- }
- if (bIsIncorrect) {
- System.out.println(ERROR_CHOICE_IS_INCORRECT);
- }
- } while (bIsIncorrect);
- sAnswer = nChoice == 1 ? sChoice1 : sChoice2;
- return sAnswer;
- }
- private static String inputPathToFile(boolean bIsInput) {
- String sPath;
- System.out.printf(MES_FORMAT_INPUT_OF_PATH, bIsInput ? "ввода" : "вывода");
- sPath = scConsole.nextLine();
- return sPath;
- }
- private static int readNumberFromConsole() {
- int nSize = 0;
- boolean bIsIncorrect;
- System.out.println(MES_INPUT_REQUEST);
- do {
- bIsIncorrect = false;
- try {
- nSize = Integer.parseInt(scConsole.nextLine());
- } catch (NumberFormatException e) {
- bIsIncorrect = true;
- System.out.print(ERROR_NOT_NUMBER + MES_TRY_AGAIN);
- }
- if (!bIsIncorrect && (nSize < MIN_SIZE || nSize > MAX_SIZE)) {
- bIsIncorrect = true;
- System.out.printf(ERROR_FORMAT_NOT_EXPECTED_NUMBER + MES_TRY_AGAIN, MIN_SIZE, MAX_SIZE);
- }
- if (!bIsIncorrect && nSize % 2 == 0) {
- bIsIncorrect = true;
- System.out.print(ERROR_NOT_ODD_NUMBER + MES_TRY_AGAIN);
- }
- } while (bIsIncorrect);
- return nSize;
- }
- public static int[][] makeMagicSquare(int n) {
- int[][] aMatrix = new int[n][n];
- int nHighMatrix = aMatrix.length - 1;
- int i = 0;
- int j = n / 2;
- int nCount = 1;
- int nWhenToStop = n * n + 1;
- do {
- aMatrix[i][j] = nCount;
- i--;
- j++;
- if (i < 0) i = nHighMatrix;
- if (j > nHighMatrix) j = 0;
- if (aMatrix[i][j] != 0) {
- i += 2;
- j--;
- }
- if (i > nHighMatrix) i -= aMatrix.length;
- if (j < 0) j = nHighMatrix;
- System.out.println("Шаг " + nCount + ":");
- for (int[] ints : aMatrix) {
- for (int k = 0; k < aMatrix.length; k++) {
- System.out.print("\t" + ints[k] + " ");
- }
- System.out.println();
- }
- nCount++;
- } while (nCount < nWhenToStop);
- return aMatrix;
- }
- private static void outputToFile(int[][] aArr, String sPathToFile) {
- boolean bOutputNotReady = true;
- do {
- try {
- PrintWriter fOutput = new PrintWriter(sPathToFile);
- fOutput.print("Размер квадрата: " + aArr.length + "x" + aArr.length);
- for (int[] ints : aArr) {
- for (int j = 0; j < aArr.length; j++) {
- fOutput.print(ints[j] + " ");
- }
- fOutput.print("\n");
- }
- fOutput.close();
- bOutputNotReady = false;
- } catch (Exception e) {
- System.out.print(ERROR_FILE_CANNOT_BE_CREATED_OR_OPENED);
- String sShouldOutputInfoToFile = choose(SYS_OP_TO_FILE_YES, SYS_OP_TO_FILE_NO, MES_ASK_OUTPUT_TO_FILE);
- if (sShouldOutputInfoToFile.equals(SYS_OP_TO_FILE_YES)) {
- sPathToFile = inputPathToFile(false);
- } else {
- bOutputNotReady = false;
- }
- }
- } while (bOutputNotReady);
- }
- private static void outputMatrix(int[][] aMagicSquare) {
- System.out.print("Матрица:\n");
- for (int[] ints : aMagicSquare) {
- for (int j = 0; j < aMagicSquare.length; j++) {
- System.out.print("\t" + ints[j] + " ");
- }
- System.out.print("\n");
- }
- String sShouldOutputInfoToFile = choose(SYS_OP_TO_FILE_YES, SYS_OP_TO_FILE_NO, MES_ASK_OUTPUT_TO_FILE);
- if (sShouldOutputInfoToFile.equals(SYS_OP_TO_FILE_YES)) {
- String sPathToFile = inputPathToFile(false);
- outputToFile(aMagicSquare, sPathToFile);
- }
- }
- public static void main(String[] args) {
- scConsole = new Scanner(System.in);
- // Путь к моему файлу вывода: /Users/sasha/Documents/___Университет/ОАиП/Пз/Лабы/блок 5/output.txt
- System.out.println(MES_TASK);
- int nSize = readNumberFromConsole();
- int[][] aMagicSquare = makeMagicSquare(nSize);
- outputMatrix(aMagicSquare);
- scConsole.close();
- }
- }
Add Comment
Please, Sign In to add comment