• Home
  • About
    • ming photo

      ming

      studying

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Archive
    • All Posts
    • All Tags
    • All categories
  • categories
    • HTML+CSS+JavaScript
    • JAVA
    • Algorithm
    • DB
    • JSP
    • 정보처리기사
    • Spring
    • Thymeleaf
    • 기술면접
  • Projects

JAVA-별찍기-6,7

15 Feb 2021

* 별찍기6(수업과제)

🔶 문제 star6

  • 코드 (풀이)
      public class star6 {
    
          public static void main(String[] args) {
    
              for(int i=1; i<=5;i++) {// 반복 횟수 : i는1부터 5까지 1씩 증가하면서 반복한다
                  //System.out.print(i);
                  for(int j=4-i;j>=0;j--) { // j는 4-i 부터 0까지 1씩 감소하면서 반복한다
                      System.out.print(" ");
                  }
                  for(int k=1; k<i*2;k++) { // k는 1부터 i*2 까지 1씩 증가하면서 반복한다
                      System.out.print("*");
                  } System.out.println();
                    
              }
              for(int i=1; i<=5;i++) { // i는 1부터 5까지 1씩증가하면서 반복한다 => 반복횟수
                  //System.out.print(i);
                  for(int j=0; j<i; j++) { // j는 0부터 i 까지 1씩증가하면서 반복한다
                      System.out.print(" ");
                  }
                  for(int q=8; q>=i*2;q--) { // q는 8부터 i*2까지 1씩 감소하며서 반복한다
                      System.out.print("*");
                  }System.out.println();
              }
                
              // 강사님 답안
      //		for (int i=0; i<=3; i++) {
      //
      //			for (int j=4-i; j>0; j--) {
      //				System.out.print(' ');
      //			} //
      //
      //			for (int j=0; j<=2*i; j++) {
      //				System.out.print('*');
      //			} //
      //
      //			System.out.println();
      //		} //
      //
      //		for (int i = 0; i <= 4; i++) {
      //
      //			for (int j = 0; j < i; j++) {
      //				System.out.print(' ');
      //			} //
      //
      //			for (int j = 2 * (4 - i); j >= 0; j--) {
      //				System.out.print('*');
      //			} //
      //
      //			System.out.println();
      //		} //
                
          }
    
      }
    

    * 별찍기7(수업과제)

    🔶 문제 star7

  • 코드 (풀이)
        
      public class star7 {
          public static void main(String[] args) {
                
          	for(int i=0; i<5; i++) { // 반복횟수
              for(int k =7; k>=i*2; k--) { // k는 7부터 i*2 까지 1씩 감소하면서 반복
                  System.out.print(" ");
              }
              for(int j=0; j<=i*4;j++) { // j 는 0부터 i*4까지 1씩 증가하면서 반복한다
                  System.out.print("*");
              }System.out.println();
          }
              // 강사님 답안
      //		 for (int i=0; i<=4; i++) {
      //			 
      //	            // for (int j=0; j<=(5-i)*2-3; j++)
      //	            for (int j=0; j<=7-2*i; j++)
      //	                System.out.print(' ');
      //	 
      //	            for (int j=0; j<=4*i; j++)
      //	                System.out.print('*');
      //	 
      //	            System.out.println();
      //	        } //
          }
    
      }
    


Share Tweet +1