Created
November 29, 2019 19:59
-
-
Save gitaficionado/6902bbabb385af2c38d3330e201b001d to your computer and use it in GitHub Desktop.
Write a method to display a pattern similar that on page 216 in the Liang textbook. The method header ispublic static void displayPattern(int n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*(Display patterns) Write a method to display a pattern as follows: | |
*1 | |
*2 1 | |
*3 2 1 | |
*... | |
*n n-1 ... 3 2 1 | |
*The method header is | |
*public static void displayPattern(int n) | |
*/ | |
public class DisplayPatterns_06_06 { | |
public static void main(String[] args) { | |
java.util.Scanner input = new java.util.Scanner(System.in); | |
System.out.print("Enter line number: "); | |
int lineNumber = input.nextInt(); | |
displayPattern(lineNumber); | |
} | |
public static void displayPattern(int n) { | |
for (int row = 1; row <= n; row++) { | |
// Print spaces | |
for (int i = row; i < n; i++) | |
System.out.print(" "); | |
// Print numbers | |
for (int i = row; i >= 1; i--) | |
System.out.print(" " + i); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment