본문 바로가기

bitcamp/JAVA

배열_연습문제

1. 로또 프로그램을 작성하시오. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Lotto {
    int[] lotto = new int[6];
    
    public Lotto() {
    }
    
    public int[] getLotto() {
        int[] lotto = new int[6];
        for (int i = 0; i < lotto.length; i++) {
            lotto[i] = (int) (Math.random() * 45+ 1;
            for (int j = 0; j < i; j++) {
                if (lotto[j] == lotto[i])
                    i--;
                break;
            }
        }
        return lotto;
    }
    
    public void getLottoNum() {
        lotto = getLotto();
        for (int i = 0; i < lotto.length; i++) {
            System.out.println(lotto[i]);
        }
    }
}
cs

 

1
2
3
4
5
6
class Lottomain {
    public static void main(String[] args) {
        Lotto lotto = new Lotto();
        lotto.getLottoNum();
    }
}
cs

 

2. 아래 main 함수를 참고하여 Box class 를 짜시오. 

· 참고

· 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Box {
    private int boxnum;
    private String content;
 
    Box(int num, String content) {
        this.boxnum = num;
        this.content = content;
    }
    public int getBoxnum() {
        return boxnum;
    }
    public String toString() {
        return content;
    }
    
    public static void main(String[] args) {
        Box[] ar = new Box[5];
        ar[0= new Box(101"Coffee");
        ar[1= new Box(202"Computer");
        ar[2= new Box(303"Apple");
        ar[3= new Box(404"Dress");
        ar[4= new Box(505"Fairy-tale book");
 
        for (Box e : ar) {
            if (e.getBoxnum() == 505)
                System.out.println(e);
        }
    }
}
cs

 

 

3. 양의 정수 10개를 랜덤생성하여 배열에 저장하고, 저장된 배열에서 3의 배수만 출력해보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Three {
    int[] num = new int[10];
    private int i;
    
    public Three() {
    }
 
    public void getThree() {
        for (int i = 0; i < num.length; i++) {
            num[i] = (int) (Math.random() * 100+ 1;
            System.out.print(num[i] + " ");
        }
        System.out.println();
    }
 
    public void getThreenum() {
        for (int i = 0; i < num.length; i++) {
            if (num[i] % 3 != 0)
                continue;
            System.out.println("3의 배수는: " + num[i]);
        }
    }
}
cs

 

1
2
3
4
5
6
7
class Threemain {
    public static void main(String[] args) {
        Three three = new Three();
        three.getThree();
        three.getThreenum();
    }
}
cs

 

4. 아래의 프로그램을 짜시오. **필수** 

· 5개의 숫자를 랜덤으로 받아 배열로 저장

· 5개의 숫자중 가장 큰값을 출력

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Maxnum {
    private int[] num = new int[5];
    private int max;
    
    public Maxnum() {
    }
    public void getMax() {
        int[] num = new int[5];
        max = num[0];
        for (int i = 0; i < num.length; i++) {
            num[i] = (int) (Math.random() * 100+ 1;
            System.out.print(num[i] + " ");
            max = max > num[i] ? max : num[i];
        }
    }
    public void getMaxnum() {
        System.out.println();
        System.out.println("최대값은: " + max);
    }
}
 
cs

 

1
2
3
4
5
6
7
class Maxnummain {
    public static void main(String[] args) {
        Maxnum maxnum = new Maxnum();
        maxnum.getMax();
        maxnum.getMaxnum();
    }
}
cs

 

5. 아래의 프로그램을 짜시오. 

· 5개의 숫자를 랜덤으로 받아 배열로 저장

· 5개의 숫자를 내림차순으로 정렬하여 출력

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Compare {
    int[] num = new int[5];
    
    public Compare() {
    }
    public void getNum() {
        for (int i = 0; i < num.length; i++) {
            num[i] = (int) (Math.random() * 100+ 1;
            System.out.print(num[i] + " ");
        }
        System.out.println();
    }
    public void getNumprint() {
        for (int i = 0; i < num.length; i++) {
            for (int j = i + 1; j < num.length; j++) {
                if (num[i] > num[j]) {
                    int change = num[i];
                    num[i] = num[j];
                    num[j] = change;
                }
            }
            System.out.print(num[i] + " ");
        }
    }
}
cs

 

1
2
3
4
5
6
7
class Comparemain {
    public static void main(String[] args) {
        Compare compare = new Compare();
        compare.getNum();
        compare.getNumprint();
    }
}
cs

 

6. char 배열을 생성하여, 알파벳 A~Z까지 대입 후, 출력해보자. (알파벳 26개)

1
2
3
4
5
6
7
8
9
10
11
class Char {
    public static void main(String[] args) {
        char[] ch = new char[26];
        //abc[0] = 65;
 
        for (int i = 0; i < 26; i++) {
            ch[i] = (char) (65 + i);
            System.out.print(ch[i] + " ");
        }
    }
}
cs

 

7. 배열과 반복문을 이용하여 프로그램을 하시오.

· 키보드에서 돈의 액수(정수)를 입력받아 오만 원권, 만 원권, 천 원권, 500원짜리 동전, 100원짜리 동전,

 50원짜리 동전, 10원짜리 동전, 1원짜리 동전이 각 몇 개로 변환되는지 아래 예시와 같이 출력하라.

 이때 반드시 다음 배열을 이용하고 반복문으로 작성하라.

· int[] unit = {50000, 10000, 1000, 500, 100, 50, 10, 1}; // 환산할 돈의 종류

· 참고

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class money {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] unit = { 5000010000100050010050101 };
 
        while (true) {
            System.out.println("금액을 입력해주세요.");
            int num = sc.nextInt();
            System.out.println(unit[0+ "원 짜리: " + num / unit[0]);
            num = num - (unit[0* (num / unit[0]));
 
            System.out.println(unit[1+ "원 짜리: " + num / unit[1]);
            num = num - (unit[1* (num / unit[1]));
 
            System.out.println(unit[2+ "원 짜리: " + num / unit[2]);
            num = num - (unit[2* (num / unit[2]));
 
            System.out.println(unit[3+ "원 짜리: " + num / unit[3]);
            num = num - (unit[3* (num / unit[3]));
 
            System.out.println(unit[4+ "원 짜리: " + num / unit[4]);
            num = num - (unit[4* (num / unit[4]));
 
            System.out.println(unit[5+ "원 짜리: " + num / unit[5]);
            num = num - (unit[5* (num / unit[5]));
 
            System.out.println(unit[6+ "원 짜리: " + num / unit[6]);
            num = num - (unit[6* (num / unit[6]));
 
            System.out.println(unit[7+ "원 짜리: " + num / unit[7]);
            num = num - (unit[7* (num / unit[7]));
        }
    }
}
cs

8. 정수를 10개 저장하는 배열 생성 후, 아래와 같이 실행하시오. **필수**

· 1에서 10까지 범위의 정수를 랜덤하게 생성하여 배열에 저장하고, 배열에 든 숫자들과 평균을 출력하라.

· 참고

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Average {
    private final int ROWS = 10;
    int[] num;
    
    public Average() {
        num = new int[ROWS];
    }
    public void getAvg() {
        for (int i = 0; i < num.length; i++) {
            num[i] = (int) (Math.random() * 10+ 1;
        }
    }
    public void getAvgnum() {
        double avg = 0;
        double total = 0;
        for (int i = 0; i < num.length; i++) {
            total = total + num[i];
            System.out.print(num[i] + " ");
        }
        avg = total / (num.length);
        System.out.println();
        System.out.println("평균은: " + avg);
    }
}
cs

 

1
2
3
4
5
6
7
class Averagemain {
    public static void main(String[] args) {
        Average average = new Average();
        average.getAvg();
        average.getAvgnum();
    }
}
cs

 

9. 4 x 4의 2차원 배열 생성 후, 아래와 같이 실행하시오. **필수**

· 1에서 10까지 범위의 정수를 랜덤하게 생성하여 배열에 저장하고, 2차원 배열을 화면에 출력하라.

· 참고

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Four {
    final int ROWS = 4;
    final int COLS = 4;
    int[][] arr;
 
    Four() {
        arr = new int[ROWS][COLS];
    }
    public void getFour() {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                int num = (int) (Math.random() * 10+ 1;
                arr[i][j] = num;
            }
        }
    }
    public void getFournum() {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}
cs

 

1
2
3
4
5
6
7
class Fourmain {
    public static void main(String[] args) {
        Four four = new Four();
        four.getFour();
        four.getFournum();
    }
}
cs

 

 

10. 아래 예시를 참고하여 과목과 점수가 짝을 이루도록 2개의 배열을 작성하라.

· 참고

 

 

·작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Cost {
    public static void main(String[] args) {
        String course[] = { "Java""C++""HTML5""컴퓨터구조""안드로이드" };
        int score[] = { 9588766255 };
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("과목 이름을 입력해주세요. ");
            String str;
            str = sc.next();
 
            if (str.equals("그만")) {
                System.out.println("종료합니다.");
                break;
            }
            for (int i = 0; i < course.length; i++) {
                if (str.equals(course[i])) {
                    System.out.println(score[i]);
                } else
                    System.out.println("없는 과목입니다.");
                break;
            }
        }
    }
}
cs

 

 

 

11. 배열을 이용하여 간단한 극장 예약 시스템을 작성해보자. 

  아주 작은 극장이라서 좌석이 10개 밖에 되지 않는다.

  사용자가 예약을 하려고 하면 먼저 좌석 배치표를 보여준다.

  예약이 끝난 좌석은 1로, 예약이 되지 않은 좌석은 0으로 나타낸다.

 

· 참고

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 class game {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] ar1 = {0123456789};
    int[] ar2 = new int[10];
        
    for(int e : ar1) {
        System.out.print(e + " ");
    }
    System.out.println();
    System.out.println("--------------------");
        
    for(int i = 0; i < ar2.length; i++) {
        System.out.print(ar2[i] + " ");        
    }
    while(true) {
        System.out.println();    
        System.out.print("몇번째 좌석을 예약하시겟습니까?");
 
        int num = sc.nextInt();        
        for(int e : ar1) {
            System.out.print(e + " ");
        }
        System.out.println();
        
        for(int i = 0; i < ar1.length; i++) {
            if(num == ar1[i]) {
                ar2[i] = 1;    
            }
        }
        System.out.println("--------------------");
        for(int i = 0; i < ar2.length; i++) {
            System.out.print(ar2[i] + " ");        
        }
      }
    }
 }
cs

'bitcamp > JAVA' 카테고리의 다른 글

클래스의 상속과 오버라이딩_연습문제  (0) 2021.01.17
클래스의 상속과 오버라이딩  (0) 2021.01.17
배열  (0) 2021.01.17
기타_연습문제  (0) 2021.01.17
메소드 오버로딩과 String 클래스  (0) 2021.01.17