Advertisement
Guest User

Untitled

a guest
Jun 21st, 2021
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class ForestRoad {
  4.  
  5.     public static void printPath(int n) {
  6.         final int height = 2 * n - 1;
  7.         String[][] forest2D = new String[height][n];
  8.  
  9.         // Filling the map with trees:
  10.         for (String[] strings : forest2D) {
  11.             Arrays.fill(strings, ".");
  12.         }
  13.  
  14.         int p = 0;
  15.         for (int i = 0; i < forest2D.length; i++) { // Iterating through each row
  16.             forest2D[i][p] = "*"; // Setting the tree to an asterisk
  17.             if (i < n - 1) { // Filling with asterisks until we reach the right border
  18.                 p++;
  19.             } else { // Returning back
  20.                 p--;
  21.             }
  22.         }
  23.  
  24.         for (int i = 0; i < forest2D.length; i++) {
  25.             for (int k = 0; k < forest2D[i].length; k++) {
  26.                 System.out.print(forest2D[i][k]);
  27.             }
  28.             if (i == forest2D.length - 1) {
  29.                 break;
  30.             } else {
  31.                 System.out.println();
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37. public class Main {
  38.     public static void main(String[] args) {
  39.         Scanner scanner = new Scanner(System.in);
  40.         ForestRoad.printPath(scanner.nextInt());
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement