Java/4. 조건문과 반복문

Day 6 : Exercise #2 - Conditional / Looping Statements

pancakemaker 2021. 10. 26. 09:56

Tue, 26th Oct 2021

package verify;

public class AsteriskOutput2_YJ {
	public static void main(String[] args) {
		int lineCount = 7;
		int spaceCount = lineCount/2; //첫 줄에 spaceCount = 3
		int starCount = 1; //첫 줄에 starCount = 1
		
		for(int i=0; i<lineCount; i++) {
			for(int j=0; j<spaceCount; j++) {
				System.out.print(' ');
			}
			for(int j=0; j<starCount; j++) {
				System.out.print("*");
			}
			for(int j=0; j<spaceCount; j++) {
				System.out.print(' ');
			}
			if(i<lineCount/2) { //0, 1, 2줄까지는 위 for문 진행 후 starCount가 증가하고 spaceCount가 감소함
				spaceCount -= 1;
				starCount += 2;
			} else if (i>=lineCount/2) { //그냥 else 해도 무관함
				//3줄부터는 위 for문 진행 후 starCount가 감소하고 spaceCount가 증가함
				spaceCount += 1;
				starCount -= 2;
			}
			System.out.println();		
		}
	}
}
   *   
  ***  
 ***** 
*******
 ***** 
  ***  
   *