본문 바로가기

프로그래밍/자바

[자바] 정올 140 ~ 149 : 반복제어문3 - 형성평가1 ~ 10

728x90
반응형
SMALL

형성평가1

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n,a, sum = 0;
        for(n = 0; n < 20; n++){
            a = sc.nextInt();
            if(a == 0) break;
            sum += a;
        }
        System.out.printf("%d %d", sum, sum/n);
    }
}

형성평가2

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n,a, sum = 0;
        for(n = 0; n < 20; n++){
            a = sc.nextInt();
            if(a == 0) break;
            sum += a;
        }
        System.out.printf("%d %d", sum, sum/n);
    }
}

형성평가3

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), count = 1;
        String star = "*";
        boolean b = false;
        while(true){
            if(count == 0 && b) break;
            System.out.println(star.repeat(count));
            if(b)count--;
            else count++;
            if(count == a) b = true;
        }
    }
}

형성평가4

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int re = 2 * n - 1;
        int blank = 0;
        boolean b = false;

        for (int i = 1; i <= 2 * n - 1; i++) {
            for (int j = 1; j <= blank; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= re; j++) {
                System.out.print("*");
            }

            if (i < n) {
                blank++;
                re -= 2;
            } else {
                blank--;
                re += 2;
            }

            System.out.println();
        }
    }
}

형성평가5

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int re = 1;
        int blank = 2*n - 2;


        for (int i = 1; i <= n; i++) {
            System.out.print(" ".repeat(blank));
            System.out.println("*".repeat(re));
            re += 2;
            blank -= 2;
        }
    }
}

형성평가6

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int all = 2*n-1;
        int blank = 2*n -2;
        int count = 1;

        for(int i = 1; i<=n; i++){
            System.out.print(" ".repeat(blank));
            for(int j = 1; j <= count; j++){
                System.out.print(j + " ");
            }
            blank-=2;
            if(blank <0) blank =0;
            count++;
            System.out.println();
        }
    }
}

형성평가7

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int init_char = 64;
        int init = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n - i;j++){
                init_char++;
                System.out.print((char)(init_char) + " ");
            }
            for(int k = 0; k < i; k++){
                System.out.print(init + " ");
                init++;
            }
            System.out.println();
        }



    }//end main
}

형성평가8

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = 1;
        for(int i = 0; i < n; i++){
            int temp = i;
            for(int j = 0; j < 2*n-1; j++){
                if((j+1) % 2 != 0 && temp == 0){
                    System.out.print(a);
                    a++;
                }
                else if((j+1) % 2 != 0 && temp != 0){
                    System.out.print(" ");
                    temp--;
                }
                else if((j+1) % 2 == 0) System.out.print(" ");
            }
            System.out.println();
        }
    }
}

형성평가9

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int blank = 0;
        int sharp = 1;

        for(int i = 1; i <= 2*n-1; i++){
            for(int k = 0; k < blank; k++){
                System.out.print("  ");
            }
            for(int j = 0; j < sharp; j++){
                System.out.print("# ");
            }
            if(i < n){
                sharp++;
            }else{
                blank++;
                sharp--;
            }
            System.out.println();
        }//end for


    }
}

형성평가10

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int print_int = 1;
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j++){
                if(print_int > 9) print_int = 1;
                System.out.print(print_int + " ");
                print_int += 2;
            }
            System.out.println();
        }
    }

}
728x90
반응형
LIST