티스토리 뷰

1. 자바

행렬(Matrix) - Lotto.java

패스트코드블로그 2020. 8. 14. 17:06
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package oop;
 
import java.util.Random;
import java.util.Scanner;
 
class Lotto{
    private int countLimit, lottoCount;
    private int[] lotto;
    private int[][] lottos;
    
    public void setCountLimit(int countLimit) {
        this.countLimit = countLimit;
    }
    public int getCountLimit() {
        return countLimit;
    }
    public void setLottos() {
        lotto = new int[6];
        lottos = new int[lottoCount][6];
        for(int i=0; i< lottoCount; i++) {
            for(int j = 0; j < 6; j++) {
                int num = (int)(Math.random() * 45+1;
                while(!isDuplication(num)) {
                    lotto[j]= num;
                    break;
                }
            }
            sort(lotto);
            System.arraycopy(lotto, 0,lottos[i], 0, lotto.length);
        }
    }
    public int[][] getLottos(){
        setLottos();
        return lottos;
    }
    
    public void sort(int[] arr) {
        for(int i=0; i< arr.length-1; i++) {
            for(int j=i+1; j< arr.length; j++) {
                if(arr[i] > arr[j]) {
                    int t = arr[i];
                    arr[i] = arr[j];
                    arr[j] = t;
                }
            }
        }
        lotto = arr;
    }
    public int lottoCount(int money) {
        int lottoCount = 0;
        if(money < 1000) {
            System.out.println("잘못된 값");
        }else {
            if(money > countLimit * 1000) {
                lottoCount = countLimit;
            }else {
                lottoCount = money / 1000;
            }
        }
        this.lottoCount = lottoCount;
        return lottoCount;
    }
    public boolean isDuplication(int num) {
        boolean ok = false;
        for(int i=0; i < lotto.length; i++) {
            if(lotto[i] == num) ok = true;
        }
        return ok;
    }
}
 
public class LottoApp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Lotto instance = new Lotto();
        System.out.println("구매한도 설정");
        instance.setCountLimit(scanner.nextInt());
        System.out.printf("설정된 구매한도 %d 장\n", instance.getCountLimit());
        while(true) {
            System.out.println("0.종료 1.로또구입");
            switch(scanner.nextInt()) {
            case 0System.out.println("종료"); return;
            case 1System.out.println("로또구입\n");buyLottos(instance,scanner);break;
            }
            
        }
    }
    public static void buyLottos(Lotto instance,Scanner scanner) {
        System.out.println("얼마치를 구입합니까? ");
        System.out.printf("로또 %d장을 발급합니다\n"
                instance.lottoCount(scanner.nextInt()));
        int[][] lottos = instance.getLottos();
        for(int i=0; i< lottos.length;i++) {
            for(int j=0; j < lottos[i].length; j++) {
                System.out.print(lottos[i][j]+"\t");
            }
            System.out.println();
        }
    }
}
cs

'1. 자바' 카테고리의 다른 글

CRUD - Person.java  (0) 2020.08.14
CRUD - Member.java  (0) 2020.08.14
OOP - ProductCart.java  (0) 2020.08.14
스프링/--/하이파/ 타이타닉 PassengerRepository.java  (0) 2020.08.07
[Madrid] Intellij Format Yaml Plugins 설정하기  (0) 2020.08.02
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함