package patterns;
import java.util.Scanner;
public class Patterns {
public static void main(String[] args) {
// Call the methods to print various patterns
printPatterns();
printHorizontalBars();
printVerticalBars();
}
// Method to print different patterns
public static void printPatterns() {
System.out.println("printPatterns() method called....");
// nested for loop to print the following pattern goes below:
// # # # # # # # #
// # # # # # # #
// # # # # # #
// # # # # #
// # # # #
// # # #
// # #
// #
// NOTE: You can copy the following nested-for loop structure and change it based on
// each of the four patterns that you need to draw
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (col <= 9 - row) {
System.out.print("# ");
} else {
System.out.print(" "); // Print space for formatting
}
}
System.out.println(); // Move to the next line
}
System.out.println(); // Extra space between patterns
// Tested for loop to print the following pattern goes below:
// # # # # # # # #
// # # # # # # #
// # # # # # #
// # # # # #
// # # # #
// # # #
// # #
// #
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row == 1 || row == 8 || col == 1 || col == 8) {
System.out.print("# ");
} else {
System.out.print(" "); // Print space for formatting
}
}
System.out.println(); // Move to the next line
}
System.out.println(); // Extra space between patterns
// nested for loop to print the following pattern goes below:
//(HINT: what do the #'s on the top and bottom have in common. What is the
// relationship between row and column values...plug in numbers for their location
// to help find the answers to the logic you need to use..think in terms of the
// row and column value)
//
// # # # # # # # #
// # #
// # #
// # #
// # #
// # #
// # #
// # # # # # # # #
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row == 1 || row == 8 || col == 1 || col == 8) {
System.out.print("# ");
} else if (row == col) {
System.out.print("# ");
} else {
System.out.print(" "); // Print space for formatting
}
}
System.out.println(); // Move to the next line
}
System.out.println(); // Extra space between patterns
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row == 1 || row == 8) {
System.out.print("# ");
} else if (col == 1 || col == 8) {
System.out.print("# ");
} else if (row + col == 9) {
System.out.print("# ");
} else {
System.out.print(" "); // Print space for formatting
}
}
System.out.println(); // Move to the next line
}
System.out.println(); // Extra space between patterns
}
// Method to print horizontal bars based on user input
public static void printHorizontalBars() {
Scanner sc = new Scanner(System.in);
int num1, num2, num3, num4;
System.out.println("printHorizontalBars() method called....");
System.out.println("Enter four integers:");
// Read the integers from the user
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
num4 = sc.nextInt();
// Print horizontal bars based on the values
printStars(num1);
printStars(num2);
printStars(num3);
printStars(num4);
System.out.println(); // Extra space after horizontal bars
}
// Helper method to print stars
private static void printStars(int count) {
for (int i = 0; i < count; i++) {
System.out.print("* "); // Print each star with a space
}
System.out.println(); // Move to the next line after printing stars
}
// Method to print vertical bars based on user input
public static void printVerticalBars() {
Scanner sc = new Scanner(System.in);
int num1, num2, num3, num4;
System.out.println("printVerticalBars() method called....");
System.out.println("Enter four integers:");
// Read the integers from the user
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
num4 = sc.nextInt();
// Find the maximum height of the bars
int max = Math.max(num1, Math.max(num2, Math.max(num3, num4)));
// Print vertical bars
for (int i = max; i > 0; i--) {
if (i <= num1) System.out.print("** "); // Building 1
else System.out.print(" "); // Space for Building 1
if (i <= num2) System.out.print("** "); // Building 2
else System.out.print(" "); // Space for Building 2
if (i <= num3) System.out.print("** "); // Building 3
else System.out.print(" "); // Space for Building 3
if (i <= num4) System.out.print("** "); // Building 4
else System.out.print(" "); // Space for Building 4
System.out.println(); // Move to the next line after each level
}
// Print a row of dashes at the bottom
System.out.println("------------------------------");
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter