gwooden_코린이

java_반복문_221123(3일차) 본문

java/3일차

java_반복문_221123(3일차)

gwooden22 2022. 11. 23. 11:35
728x90

★풀이01

 

package study;

 

import java.util.Scanner;

 

public class Sample01 {

 

        public static void main(String[] args) {

              

               //1. 숫자를 입력 (정수)

               //2. 1부터 입력받은 정수까지 나눠봄

               //3. 입력받은 정수 / 1 ~ 입력정수까지 -> for

               //4. 나눠덜어지는 값의 개수

               //5. 개수가 2개면 -> 소수

               //6. 아니면? 소수아님

              

               int num1; //정수를 받을 변수

               int i; //증감식에 이용할 변수

               int cnt = 0; //값을 저장해둘 변수

              

               Scanner sc = new Scanner(System.in);

              

               System.out.print("정수를 입력: "); //i 13 줬을 경우

               num1 = sc.nextInt();

              

               for(i = 1; i <= num1; i++) { //i 1이고 i num1 보다 같거나 작으면 종료

                      

                       if(num1 % i == 0) { //예를 들어 num1 13 13 % 1 시작해서 i num1

                                                             //만족할 까지 계속반복해서 떨어지지 않는 값을 도출

                             

                                                             //i    cnt     num1

                                                             //1     0       7 

                                                             //2     1       7 

                                                             //3     1       7 

                                                             //4     1       7 

                                                             //5     1       7 

                                                             //6     1       7 

                                                             //7     1       7 

                                                             //8     2       7 i num1 보다 작거나 같지 않다 보니 종료

                             

                              cnt++;

                              //마지막에서 1 나오고 증감식이 있으니 1 += 1 되면서 최종 cnt 값은 2 된다.

                             

                              }

                      

                       }

//                            System.out.println(cnt);

              

                       if(cnt==2) {

                              System.out.println("입력받은 " + num1 + " 정수는 소수");

                       } else {

                              System.out.println("입력받은 " + num1 + " 소수가 아니다.");

                      

                             

               }

 

        }

 

}

 

 


★풀이02

package study;

 

import java.util.Scanner;

 

public class Sample02 {

 

         public static void main(String[] args) {

                 

                  //3 배수 이면서 2 배수가 아닌 if

                  //&& 사용

                 

                  int cnt = 0;

                  int sum = 0;

                  int i;

                 

                  for(i = 1; i <= 100; i++) { // 1 ~ 100까지, 1 증가

                                              // i 100 보다 크면 조건식

                          

                           if(i % 3==0 && i % 2 != 0) {

                           //   3 배수          2의배수

                                    cnt++;

                                    // 의에 조건을 만족하는 경우에만 cnt 값은 계속 증가

                                   

                                    sum = sum + i;

                                    // 조건이 만족할때 마다 sum 값이 증가

                                   

                                    System.out.print(i + " ");

                                    //가독성을 위해 큰따옴표 띄어쓰기 하면 가동성이 좋아진다.

                                   

                           }

                          

                  }

//                System.out.println(); <-- 프린트ln 아무런 조건식 없이

사용하면 단순 줄바꿈용으로 사용 가능

                 

                  System.out.println("\n합계: " + sum);

                  //

                 

                  System.out.println("개수 : " + cnt);

                 

         }

 

}


★풀이03

 

for(이중 반복문)

)

1 ~ 5까지를 3번 출력

 

12345

12345

12345

 

package study;

 

public class Sample03 {

 

         public static void main(String[] args) {

 

                  int i, j;

                 

                  for(j=1; j<=3; j++) {

                          

                           for(i=1; i<=5; i++) {

                                    System.out.print(i + " ");

                           }

                           System.out.println();

                  }

 

         }

 

}


J                 i

1                1

                  2

                  3

                  4

                  5

                  6 <- 종료

2                1

                  2

                  3

                  4

                  5

                  6 <-종료

3                1

                  2

                  3

                  4

                  5

                  6 <-종료

4<-종료

 


★풀이03-1

package study;

 

public class Sample04 {

 

        public static void main(String[] args) {

               //구구단 출력

               //2 ~ 9

              

               int i, j;

              

                

              

                       for(i=2; i<=9; i++) {

                             

                              for(j=1; j<=9; j++) {

                                     

                                      System.out.print(i + "*" + j +  " = " + i * j + " ");

                                     

                              }

                              System.out.println();

                       }

       

        }

 

}


★풀이03-2

 

package study;

 

public class Sample05 {

 

        public static void main(String[] args) {

               //구구단 출력

               //2 ~ 9

              

               int i, j;

               

              

                

              

                       for(i=1; i<=9; i++) {

                             

                              for(j=2; j<=9; j++) {

                                     

                                      System.out.print(j + "*" + i +  " = " + j * i + " ");

                                     

              

                              }

                             

                              System.out.println();

                       }

       

        }

 

}


★풀이03-3

 

package study;

 

public class Sample05 {

 

        public static void main(String[] args) {

               //구구단 출력

               //2 ~ 9

              

               int i, j;

               

              

                

              

                       for(i=1; i<=9; i++) {

                             

                              for(j=2; j<=9; j++) {

                                     

                                      System.out.print(j + "*" + i +  " = " + j * i + " ");

                                     

              

                              }

                             

                              System.out.println();

                       }

       

        }

 

}

 

 

package study;

 

public class Sample05 {

 

        public static void main(String[] args) {

               //구구단 출력

               //2 ~ 9

              

               int i, j;

                

              

                

              

                       for(i=1; i<=9; i++) {

                             

                              for(j=2; j<=9; j++) {

                                     

                                      System.out.printf("%3d * %d = %3d", j, i, j*i);

                                      //%'숫자'd에서 숫자는 공간을 마련해주는 3이면 3공간을 차지 결과 도출

                                     

//                                    System.out.print(j + "*" + i +  " = " + j * i + " ");

//                                    System.out.print(j + "*" + i +  " = " + j * i + " \t"); <--또는 이 조건식으로도 깔끔한 정리가 가능하다.

                                     

              

                              }

                             

                              System.out.println();

                       }

       

        }

 

}


★풀이04-1

 

package study;

 

import java.util.Scanner;

 

public class Sample06 {

 

        public static void main(String[] args) {

               //ex) 입력 : 2

               //**

               //**

              

               // ex) 입력 : 5

               //*****

               //*****

               //*****

               //*****

               //*****

              

               int num1;

               int i, e;

              

               Scanner sc = new Scanner(System.in);

              

              

               System.out.print("입력하고 싶은 정수 : ");

               num1 = sc.nextInt(); //원하는 정수를 입력하면 정수는 num1 변수에 들어간다.

                                                             //이때 num1 입력한 정수를 담고 있다.

              

               for(i=1; i<=num1; i++){ //i 1 num1 보다 같거나 작을때 까지 계속 증가

                      

                       for(e=1; e<=num1; e++) { // 번째 for문에서 조건이 만족할 번째 for문으로 내려와 증감식 시작

                                                                            //e 1 e num1 보다 같거나 작을때 까지 계속 증가

                                                                            //e num1 조건식에 도달하면 다시 번째 for문으로 돌아간다.

                                                                            // 번째 for문으로 돌아가서 다시 조건식에 맞게 증가 시작

                                                                            //반복~

                              System.out.print('*');

                       }

                      

                       System.out.println();

               }

              

 

        }

 

}


★풀이04-2

 

package study;

 

import java.util.Scanner;

 

public class Sample06 {

 

        public static void main(String[] args) {

               //ex) 입력 : 2

               //*

               //**

              

               // ex) 입력 : 5

               //*

               //**

               //***

               //****

               //*****

              

               int num1;

               int i, e;

               Scanner sc = new Scanner(System.in);

              

              

               System.out.print("입력하고 싶은 정수 : ");

               num1 = sc.nextInt(); //원하는 정수를 입력하면 정수는 num1 변수에 들어간다.

                                                             //이때 num1 입력한 정수를 담고 있다.

              

               for(i=1; i<=num1; i++){ //i 1 num1 보다 같거나 작을때 까지 계속 증가

                      

                      

                               for(e=1; e<=i; e++) { //조건식 e 1 아닌 i변수를 넣고 조건식에서 e 변수 정수 변수를 넣으면

                                                                            //* 위에서 부터 수량 부터 찍혀서 출력된다.

//                                                                          for(e=i; e<=num1; e++)

                             

              

                             

                              System.out.print('*');

                             

                             

                       }

                      

                       System.out.println();

               }

              

 

        }

 

}


★풀이04-3

 

package study;

 

import java.util.Scanner;

 

public class Sample06 {

 

        public static void main(String[] args) {

               //ex) 입력 : 2

               //**

               // *

              

               // ex) 입력 : 5

               //*****

               // ****

               //  ***

               //   **

               //    *

              

               int num1;

               int i;

//             int e;

               int k;

               Scanner sc = new Scanner(System.in);

              

              

               System.out.print("입력하고 싶은 정수 : ");

               num1 = sc.nextInt(); //원하는 정수를 입력하면 정수는 num1 변수에 들어간다.

                                                             //이때 num1 입력한 정수를 담고 있다.

              

               for(i=1; i<=num1; i++){

                      

                       for(k=1; k<=i-1; k++) {

                              System.out.print(" ");

                       }

                      

//                            for(e=i; e<=num1; e++) { <-- 변수를 하나 굳이 만들 필요는 없다.

                                                                       // 이유로는 번째 k변수는 어차피 만족이 안되어 빈공간만 남게 되니...

                              for(k=1; k<=num1+1 - i; k++) {

                                              System.out.print('*');

                                      }

                             

                                      System.out.println();

                       }

                      

               }

              

 

}

 

 

728x90

'java > 3일차' 카테고리의 다른 글

java_배열_221123(3일차)  (0) 2022.11.23
java_do ~ while문_221123(3일차)  (0) 2022.11.23
java_while문_221123(3일차)  (0) 2022.11.23
Comments