codewars063 - Give me a Diamond

Instructions

https://www.codewars.com/kata/5503013e34137eeeaa001648/train/java

Solution:

class Diamond {
  public static String print(int n) {

		if (n % 2 == 0 || n < 0) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		int count = n / 2;
		for (int i = count; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				sb.append(" ");
			}
			for (int j = 0; j < n - i * 2; j++) {
				sb.append("*");
			}
			sb.append("\n");

		}
		for (int i = 0; i < n; i++) {
			sb.append("*");
		}
		sb.append("\n");
		for (int i = 1; i <= count; i++) {
			for (int j = 0; j < i; j++) {
				sb.append(" ");
			}
			for (int j = 0; j < n - i * 2; j++) {
				sb.append("*");
			}
			sb.append("\n");

		}

		return sb.toString();

	    
    
	}
}

Sample Test:

https://www.codewars.com/kata/5503013e34137eeeaa001648/train/java
相關文章
相關標籤/搜索