Java for loop display pattern 8

Question

We would like to write a method to display a pattern as follows:

 Pre:
 pattern:
                   1
                 2 1
               3 2 1
     ...
     n n-1 ... 3 2 1

The method header is

public static void displayPattern(int n)


import java.util.Scanner;

public class Main {
  /** Main Method */
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in); // Create a Scanner

    // Prompt the user to enter the number of rows
    System.out.print("Enter the number of rows: ");
    int numberOfRows = input.nextInt();

    // Display pattern
    displayPattern(numberOfRows);/*from ww w . j ava 2s  .  c  o  m*/
  }

  /** displayPattern Method */
  public static void displayPattern(int n) {
    
    //your code here
    
  }
}




import java.util.Scanner;

public class Main {
  /** Main Method */
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in); // Create a Scanner

    // Prompt the user to enter the number of rows
    System.out.print("Enter the number of rows: ");
    int numberOfRows = input.nextInt();

    // Display pattern
    displayPattern(numberOfRows);
  }

  /** displayPattern Method */
  public static void displayPattern(int n) {
    int padding = n - 1;  // Holds number of whitespace
    for (int r = 1; r <= n; r++) {
      // Print padding
      for (int p = 0; p < padding; p++) {
        System.out.print("  ");
      }

      // Print numbers
      for (int i = r; i > 0; i--) {
        System.out.print(i + " ");
      }
      System.out.println();
      padding--; // Decrement padding
    }
  }
}



PreviousNext

Related