Advertisement
AntonS2000

Untitled

Oct 30th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.92 KB | None | 0 0
  1. package com.company;
  2.  
  3. /*
  4.     task 5
  5.     problem 7
  6.  */
  7.  
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class Main {
  12.  
  13.     public static final String INPUT_MESSAGE = "enter n:";
  14.     public static final String FIRST_ERROR_MESSAGE = "input error";
  15.     public static final String SECOND_ERROR_MESSAGE = "error: n is odd";
  16.  
  17.     public static int getInt(Scanner scanner){
  18.         System.out.println(INPUT_MESSAGE);
  19.         while(true){
  20.  
  21.  
  22.             while(!scanner.hasNextInt()){
  23.                 System.out.println(FIRST_ERROR_MESSAGE);
  24.                 scanner.next();
  25.             }
  26.             int a = scanner.nextInt();
  27.  
  28.             if(a%2==1){
  29.                 return a;
  30.             }
  31.             else{
  32.                 System.out.println(SECOND_ERROR_MESSAGE);
  33.             }
  34.         }
  35.  
  36.  
  37.  
  38.     }
  39.  
  40.  
  41.     public static void draw(int n, char bg, char fg){
  42.         for(int i = 0; i < n/2; i++){
  43.             for(int j = 0; j < (n-(1+2*i))/2; j++){
  44.                 System.out.print(bg);
  45.             }
  46.             for(int j = 0; j < 1+2*i;j++){
  47.                 System.out.print(fg);
  48.             }
  49.             for(int j = 0; j < (n-(1+2*i))/2; j++){
  50.                 System.out.print(bg);
  51.             }
  52.             System.out.println();
  53.         }
  54.         for(int j = 0; j < n;j++){
  55.             System.out.print(fg);
  56.         }
  57.         System.out.println();
  58.  
  59.         for(int i = n/2-1; i >= 0; i--){
  60.             for(int j = (n-(1+2*i))/2-1; j >= 0; j--){
  61.                 System.out.print(bg);
  62.             }
  63.             for(int j = 0; j < 1+2*i;j++){
  64.                 System.out.print(fg);
  65.             }
  66.             for(int j = (n-(1+2*i))/2-1; j >= 0; j--){
  67.                 System.out.print(bg);
  68.             }
  69.             System.out.println();
  70.         }
  71.     }
  72.  
  73.     public static void main(String[] args) {
  74.         Scanner scanner = new Scanner(System.in);
  75.  
  76.         int n = getInt(scanner);
  77.  
  78.         draw(n,'.','#');
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement