JAVA/이론 정리 및 예제

[JAVA/자바] #4_4 배열 실습문제

chaewon 2021. 9. 14. 14:24
package com.kh.arrayPractice2.sample;

import java.util.Random;
import java.util.Scanner;

public class ArraySample {
        public void test1() {
                int[] iarr = new int[10];
                
                for(int i = 0; i < iarr.length; i++) {
                        iarr[i] = i;
                        System.out.println("iarr[" + i + "] : " + iarr[i]);
                }
        }
        public void test2() {
                String[] sarr = new String[] {"딸기", "바나나", "복숭아", "키위", "사과"};
                
                System.out.println("sarr[1] : " + sarr[1]);
                
        }
        public void test3() {
                Scanner sc = new Scanner(System.in);
                System.out.print("문자열 입력 : ");
                String str = sc.nextLine();
                
                char[] carr = new char[str.length()];
                
                for(int i = 0; i < str.length(); i++) {
                        carr[i] = str.charAt(i);
                }
                
                System.out.print("문자 입력 : ");
                char ch = sc.next().charAt(0);
                int count = 0;
                for(int i = 0; i < carr.length; i++) {
                        if(str.charAt(i) == ch) {
                                count++;
                        }
                }
                System.out.println("입력하신 문자열 " + str + "에서 찾으시는 문자 " + ch + "은 " + count + "개 입니다.");
        }
        public void test4() {
                Scanner sc = new Scanner(System.in);
                System.out.print("주민등록번호 입력 : ");
                String str1 = sc.nextLine();
                
                String[] id = new String[] {str1};

                char[] copyId = new char[str1.length()];
                
                for(int i = 0; i < str1.length(); i++) {
                        copyId[i] = str1.charAt(i);
                }
                
                String str2 = "";
                for(int i = 0; i < id.length; i++) {
                                str2 += id[i];
                }
                System.out.println("id : " + str2);
                
                str2 = "";
                for(int i = 0; i < str1.length(); i++) {
                        if(i > str1.length() - 7) {
                                str2 += "*";
                        }else {
                                str2 += copyId[i];
                        }
                }
                System.out.println("copyId : " + str2);
        }
        public void test5() {
                Scanner sc = new Scanner(System.in);
                System.out.print("홀수인 정수 입력 : ");
                int num = sc.nextInt();
                
                int[] ir = new int[num+(num-1)];
                
                if(num % 2 != 0) {
                        for(int i = 0; i < ir.length; i++) {
                                if(i > num - 2) {
                                        ir[i] = num--;
                                }else {
                                        ir[i] = i+1;
                                }
                                System.out.println("ir[" + i + "] : " + ir[i]);
                        }
                }else {
                        System.out.println("홀수인 정수를 입력하세요.");
                }
        }
        public void test6() {
                Random r = new Random();
                int[] rotto = new int[7];
                
                for(int i = 0; i < rotto.length; i++) {
                        rotto[i] = r.nextInt(45) + 1;
                        for(int j = 0; j < i; j++) {
                                if(rotto[i] == rotto[j]) {
                                        i--;                        
                                }
                        }
                }
                for(int i = 1; i < rotto.length; i++) {
                        for(int j = 0; j < i; j++) {
                                if(rotto[i] < rotto[j]) {
                                        int temp;
                                        temp = rotto[i];
                                        rotto[i] = rotto[j];
                                        rotto[j] = temp;
                                }
                        }
                }
                System.out.print("rotto 번호 : ");
                for(int i = 0; i < rotto.length; i++) {
                        if(i < rotto.length - 1) {
                                System.out.print(rotto[i] + ", ");
                        }else {
                                System.out.print(rotto[i]);
                        }
                }
        }
        
}