Skip to content

Instantly share code, notes, and snippets.

@Izzur
Created May 8, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Izzur/19534f17845f94abae7d61198026b184 to your computer and use it in GitHub Desktop.
Save Izzur/19534f17845f94abae7d61198026b184 to your computer and use it in GitHub Desktop.
Draw some pattern for fun
public class pattern1 {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
if(n == 1) System.out.println("x "); else
printPattern(n);
}
private static void printPattern(int n) {
boolean genap = (n%2 == 0) ? true : false;
String basePattern = "";
for (int i=0; i<(genap ? (n/2) : (n/2+1)); i++) {
basePattern += (i%2 == 0) ? "x " : "o ";
}
int length = basePattern.length();
/* Pattern Top */
for (int i=0; i<n-1; i++) {
for (int j=0; j<n-1; j++) {
System.out.print(" ");
}
for (int j=0+length-i-1; j<length; j++) {
System.out.print(basePattern.charAt(length-j-1));
}
System.out.println("");
}
/* Pattern Mid */
System.out.print(basePattern.trim());
for (int i=(genap ? 0 : 2); i<length; i++) {
System.out.print(basePattern.charAt(length-i-1));
}
System.out.println();
/* Pattern Bot */
for (int i=0; i<n-1; i++) {
for (int j=0; j<i+1; j++) {
System.out.print(" ");
}
System.out.println(basePattern.substring(0,length-1-i-(n%2)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment