CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 18:26:28 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=24d80e793b0d255bef0ef9e7fdccaa08c7896cdcf1e2fbaf74f11495990e2ce0a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22QPoZraYFxP7VJKAhKq5cg71qEqvAYVgN%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d8a5467f488087-BLR
Java23 - Pastebin.com
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.file.*;
- import java.util.Scanner;
- public class Main {
- final static int MIN_N = 2;
- final static int MAX_N = 100;
- final static int MIN_A = -10000;
- final static int MAX_A = 10000;
- static Scanner in = new Scanner(System.in);
- public static int inputAndCheckChoice() {
- boolean isIncorrect;
- isIncorrect = false;
- int choice;
- choice = 0;
- do {
- System.out.println("Введите ваш выбор.");
- try {
- choice = Integer.parseInt(in.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- System.err.println("Некорректный ввод.");
- }
- if (choice != 1 && choice != 2) {
- System.err.println("Введите 1 или 2.");
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- return choice;
- }
- public static int inputAndCheckN() {
- boolean isIncorrect;
- int n;
- n = 0;
- do {
- isIncorrect = false;
- System.out.println("Введите порядок матрицы n: ");
- try {
- n = Integer.parseInt(in.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- System.err.println("Некорректный ввод. Введите целое число.");
- }
- if (!isIncorrect && (n < MIN_N || n > MAX_N)) {
- isIncorrect = true;
- System.err.println("Диапазон значений n от " + MIN_N + " до " + MAX_N + ".");
- }
- } while (isIncorrect);
- return n;
- }
- public static boolean isEof(final String fileName) {
- boolean isEof;
- isEof = false;
- try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
- String line = br.readLine();
- if (line != null) {
- isEof = true;
- }
- } catch (IOException e) {
- System.err.println("Ошибка.");
- }
- return isEof;
- }
- public static String requestFileNameForReading() {
- boolean isIncorrect;
- String fileName;
- isIncorrect = true;
- File tempFile;
- Path path;
- do {
- System.out.println("Введите относительный путь к файлу.");
- fileName = in.nextLine();
- path = Paths.get(fileName);
- tempFile = new File(fileName);
- if (!tempFile.exists()) {
- System.err.println("Файл не существует.");
- } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
- System.err.println("Файл не является текстовым.");
- } else if (!Files.isReadable(path)) {
- System.err.println("Этот файл невозможно открыть для чтения.");
- } else if (!isEof(fileName)) {
- System.err.println("Этот файл пустой.");
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- return fileName;
- }
- public static String requestFileNameForWriting() {
- boolean isIncorrect;
- String fileName;
- isIncorrect = true;
- File tempFile;
- Path path;
- do {
- System.out.println("Введите относительный путь к файлу.");
- fileName = in.nextLine();
- path = Paths.get(fileName);
- tempFile = new File(fileName);
- if (!tempFile.exists()) {
- System.err.println("Файл не существует.");
- } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
- System.err.println("Файл не является текстовым.");
- } else if (!Files.isWritable(path)) {
- System.err.println("Этот файл невозможно открыть для записи.");
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- return fileName;
- }
- public static int inputSizeFromFile(String fileName) {
- boolean isIncorrect;
- int size;
- isIncorrect = false;
- do {
- size = 0;
- try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
- String line = br.readLine();
- if (line != null) {
- size = Integer.parseInt(line.trim());
- isIncorrect = (size < MIN_N) || (size > MAX_N);
- if (isIncorrect) {
- System.out.println("Размер не входит в диапазон от " + MIN_N + " до " + MAX_N);
- isIncorrect = false;
- fileName = requestFileNameForReading();
- }
- } else {
- System.out.println("Файл пуст.");
- isIncorrect = true;
- fileName = requestFileNameForReading();
- }
- } catch (IOException e) {
- System.out.println("Ошибка при чтении файла: ");
- isIncorrect = true;
- fileName = requestFileNameForReading();
- } catch (NumberFormatException e) {
- System.out.println("Ошибка в первой строке. Неправильный размер матрицы");
- isIncorrect = true;
- fileName = requestFileNameForReading();
- }
- } while (isIncorrect);
- return size;
- }
- public static double[][] readMatrix(int size, String fileName) {
- double[][] matrix = new double[size][size];
- boolean isIncorrect;
- String[] doubleInString;
- String tempLine;
- do {
- isIncorrect = false;
- try (BufferedReader fReader = new BufferedReader(new FileReader(fileName))) {
- fReader.readLine();
- if (matrix != null) {
- for (int i = 0; i < size; i++) {
- tempLine = fReader.readLine();
- if (tempLine != null) {
- doubleInString = tempLine.split(" ");
- for (int j = 0; j < size; j++) {
- matrix[i][j] = Double.parseDouble(doubleInString[j]);
- if (matrix[i][j] < MIN_A || matrix[i][j] > MAX_A) {
- isIncorrect = true;
- }
- }
- }
- }
- if (isIncorrect) {
- System.err.println("Элементы матрицы выходят за диапазон допустимых значений.");
- }
- } else {
- System.err.println("Матрица не содержит элементов.");
- isIncorrect = true;
- }
- } catch (NumberFormatException e) {
- System.err.println("Некорректный тип данных.");
- isIncorrect = true;
- matrix = null;
- } catch (IOException e) {
- System.err.println("Непредвиденная ошибка.");
- isIncorrect = true;
- matrix = null;
- }
- if (isIncorrect) {
- fileName = requestFileNameForReading();
- size = inputSizeFromFile(fileName);
- }
- } while (isIncorrect);
- return matrix;
- }
- public static double[][] createMatrix(final int n) {
- double[][] matrix = new double[n][n];
- boolean isIncorrect;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- do {
- isIncorrect = false;
- System.out.println("Введите элемент матрицы: ");
- try {
- matrix[i][j] = Double.parseDouble(in.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- System.err.println("Некорректный ввод. Введите число.");
- }
- if (!isIncorrect && (matrix[i][j] < MIN_A || matrix[i][j] > MAX_A)) {
- isIncorrect = true;
- System.err.println("Диапазон значений A от " + MIN_A + " до " + MAX_A + ".");
- }
- } while (isIncorrect);
- }
- }
- return matrix;
- }
- public static double sumElements(double[][] matrix, int n) {
- double sum;
- sum = 0.0;
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < i; j++) {
- if (matrix[i][j] > 0) {
- sum += matrix[i][j];
- }
- }
- }
- return sum;
- }
- public static void printToConsole(final double sum) {
- System.out.println("Сумма равна " + sum);
- }
- public static boolean printToFile(String fileName, final double sum) {
- boolean isIncorrect;
- isIncorrect = false;
- String result = Double.toString(sum);
- do {
- try (BufferedWriter fWriter = new BufferedWriter(new FileWriter(fileName))) {
- fWriter.write(result);
- System.out.println("Результат выведен в файле.");
- } catch (IOException e) {
- System.err.println("Непредвиденная ошибка.");
- isIncorrect = true;
- fileName = requestFileNameForReading();
- }
- } while (isIncorrect);
- return isIncorrect;
- }
- public static void main(String[] args) {
- int choice;
- int n;
- double sum;
- boolean isIncorrect;
- String fileName;
- System.out.println("Данная программа считает сумму положительных элементов матрицы порядка n, стоящих под главной диагональю.\nДиапазон значений порядка матрицы n: "
- + MIN_N + "..." + MAX_N + "\nДиапазон для значений элементов матрицы : " + MIN_A + "..." +
- MAX_A + "\nОткуда вы хотите вводить данные ? \n1 - консоль, 2 - файл.");
- choice = inputAndCheckChoice();
- double[][] matrix;
- if (choice == 1) {
- n = inputAndCheckN();
- matrix = createMatrix(n);
- } else {
- fileName = requestFileNameForReading();
- n = inputSizeFromFile(fileName);
- matrix = readMatrix(n, fileName);
- }
- sum = sumElements(matrix, n);
- System.out.println("Куда Вы хотите вывести результат? \n1 - консоль, 2 - файл.");
- choice = inputAndCheckChoice();
- if (choice == 1) {
- printToConsole(sum);
- } else {
- do {
- fileName = requestFileNameForWriting();
- isIncorrect = printToFile(fileName, sum);
- } while (isIncorrect);
- }
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
-
🚀 Swapzone +37% glitch
JavaScript | 1 sec ago | 0.24 KB
-
📌 Swapzone +37% glitch ⭐ 5
JavaScript | 6 sec ago | 0.25 KB
-
⚡ Crypto Swap Glitch ✅ Working ⚡
JavaScript | 10 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ⭐⭐ 4
JavaScript | 18 sec ago | 0.25 KB
-
💎 ChangeNOW Exploit
JavaScript | 22 sec ago | 0.24 KB
-
✅ Marketplace Glitch ✅ Working NEVER SEEN BE...
JavaScript | 28 sec ago | 0.25 KB
-
🚨 Free Crypto Method 🚨
JavaScript | 32 sec ago | 0.24 KB
-
💡 EASY MONEY GUIDE ✅ Working
JavaScript | 42 sec ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand