Created
May 8, 2017 14:43
-
-
Save Izzur/19534f17845f94abae7d61198026b184 to your computer and use it in GitHub Desktop.
Draw some pattern for fun
This file contains hidden or 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
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