Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class lab2_5 {
- static Scanner scConsole = new Scanner(System.in);
- private static PrintWriter getOutputFileLocation() {
- boolean isIncorrect;
- String location;
- PrintWriter file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new PrintWriter(location);
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static Scanner getInputFileLocation() {
- boolean isIncorrect;
- String location;
- Scanner file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new Scanner(new File(location));
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static void createTriangle(int size, int[][] triangle) {
- for (int n = 0; n <= size; n++) {
- triangle[n][0] = triangle[n][n] = 1;
- for (int k = 1; k < n; k++) {
- triangle[n][k] = triangle[n - 1][k - 1] + triangle[n - 1][k];
- }
- }
- }
- private static void resultPrint(int[][] triangle, int size) {
- for (int i = 0; i <= size; i++) {
- for (int j = 0; j <= i; j++) {
- System.out.print(triangle[i][j] + " ");
- }
- System.out.println();
- }
- }
- private static int getSizeFromConsole() {
- final int MIN_VALUE = 1;
- final int MAX_VALUE = 30;
- int size = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- size = Integer.parseInt(scConsole.nextLine());
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if ((!isIncorrect) && ((size < MIN_VALUE) || (size > MAX_VALUE))) {
- System.out.println("Please enter a natural value less than 30");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return size;
- }
- private static int getSizeFromFile() {
- final int MIN_VALUE = 1;
- final int MAX_VALUE = 30;
- boolean isIncorrect = false;
- int size = 0;
- Scanner file = getInputFileLocation();
- do {
- try {
- size = file.nextInt();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Enter natural number");
- }
- if ((!isIncorrect) && ((size < MIN_VALUE) || (size > MAX_VALUE))) {
- System.out.println("Please enter a natural value less than 30");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return size;
- }
- private static int chooseInput() {
- boolean isIncorrect;
- String line;
- do {
- isIncorrect = false;
- System.out.println("Do you want to input from file? (y/n)");
- line = scConsole.nextLine().toLowerCase();
- if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
- isIncorrect = true;
- System.out.println("Enter valid answer");
- }
- } while (isIncorrect);
- if (line.equals("y") || line.equals("")) {
- return 0;
- } else {
- return 1;
- }
- }
- private static void outputToFile(int[][] triangle, int size) {
- PrintWriter out = getOutputFileLocation();
- for (int i = 0; i <= size; i++) {
- for (int j = 0; j <= i; j++) {
- out.print(triangle[i][j] + " ");
- }
- out.println();
- }
- out.close();
- }
- public static void main(String[] args) {
- int[][] triangle;
- int size;
- System.out.println("Данная программа строит треугольник Паскаля заданного размера.");
- int chosenInput = chooseInput();
- System.out.println("Enter the size of Pascal`s triangle");
- if (chosenInput == 0) {
- size = getSizeFromFile();
- } else {
- size = getSizeFromConsole();
- }
- triangle = new int[size + 1][size + 1];
- createTriangle(size, triangle);
- System.out.println("Triangle:");
- resultPrint(triangle, size);
- outputToFile(triangle, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement