๋ฌธ์
- ๊ธฐ๋ฅ ์๊ตฌ ์ฌํญ
(*์ฐ์ํ ํ ํฌ์ฝ์ค ํ๋ฆฌ์ฝ์ค์ ๋ฌธ์ ์์ ์๊ตฌ์ฌํญ๋ค์ ๋ช ๊ฐ ์ ์ธํ์ฌ ํ์์ต๋๋ค.)
๋ก๋ ๊ฒ์ ๊ธฐ๋ฅ์ ๊ตฌํํด์ผ ํ๋ค. ๋ก๋ ๊ฒ์์ ์๋์ ๊ฐ์ ๊ท์น์ผ๋ก ์งํ๋๋ค.
- ๋ก๋ ๋ฒํธ์ ์ซ์ ๋ฒ์๋ 1~45๊น์ง์ด๋ค.
- 1๊ฐ์ ๋ก๋๋ฅผ ๋ฐํํ ๋ ์ค๋ณต๋์ง ์๋ 6๊ฐ์ ์ซ์๋ฅผ ๋ฝ๋๋ค.
- ๋น์ฒจ ๋ฒํธ ์ถ์ฒจ ์ ์ค๋ณต๋์ง ์๋ ์ซ์ 6๊ฐ์ ๋ณด๋์ค ๋ฒํธ 1๊ฐ๋ฅผ ๋ฝ๋๋ค.
- ๋น์ฒจ์ 1๋ฑ๋ถํฐ 5๋ฑ๊น์ง ์๋ค. ๋น์ฒจ ๊ธฐ์ค๊ณผ ๊ธ์ก์ ์๋์ ๊ฐ๋ค.
- 1๋ฑ: 6๊ฐ ๋ฒํธ ์ผ์น / 2,000,000,000์
- 2๋ฑ: 5๊ฐ ๋ฒํธ + ๋ณด๋์ค ๋ฒํธ ์ผ์น / 30,000,000์
- 3๋ฑ: 5๊ฐ ๋ฒํธ ์ผ์น / 1,500,000์
- 4๋ฑ: 4๊ฐ ๋ฒํธ ์ผ์น / 50,000์
- 5๋ฑ: 3๊ฐ ๋ฒํธ ์ผ์น / 5,000์
```
- ๋ก๋ ๊ตฌ์
๊ธ์ก์ ์
๋ ฅํ๋ฉด ๊ตฌ์
๊ธ์ก์ ํด๋นํ๋ ๋งํผ ๋ก๋๋ฅผ ๋ฐํํด์ผ ํ๋ค.
- ๋ก๋ 1์ฅ์ ๊ฐ๊ฒฉ์ 1,000์์ด๋ค.
- ๋น์ฒจ ๋ฒํธ์ ๋ณด๋์ค ๋ฒํธ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
- ์ฌ์ฉ์๊ฐ ๊ตฌ๋งคํ ๋ก๋ ๋ฒํธ์ ๋น์ฒจ ๋ฒํธ๋ฅผ ๋น๊ตํ์ฌ ๋น์ฒจ ๋ด์ญ ๋ฐ ์์ต๋ฅ ์ ์ถ๋ ฅํ๊ณ ๋ก๋ ๊ฒ์์ ์ข
๋ฃํ๋ค.
์คํ ๊ฒฐ๊ณผ ์์
```
๊ตฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.
8000
8๊ฐ๋ฅผ ๊ตฌ๋งคํ์ต๋๋ค.
[8, 21, 23, 41, 42, 43]
[3, 5, 11, 16, 32, 38]
[7, 11, 16, 35, 36, 44]
[1, 8, 11, 31, 41, 42]
[13, 14, 16, 38, 42, 45]
[7, 11, 30, 40, 42, 43]
[2, 13, 22, 32, 38, 45]
[1, 3, 5, 14, 22, 45]
๋น์ฒจ ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.
1,2,3,4,5,6
๋ณด๋์ค ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.
7
๋น์ฒจ ํต๊ณ
---
3๊ฐ ์ผ์น (5,000์) - 1๊ฐ
4๊ฐ ์ผ์น (50,000์) - 0๊ฐ
5๊ฐ ์ผ์น (1,500,000์) - 0๊ฐ
5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น (30,000,000์) - 0๊ฐ
6๊ฐ ์ผ์น (2,000,000,000์) - 0๊ฐ
์ด ์์ต๋ฅ ์ 62.5%์
๋๋ค.
ํด๋์ค ๊ตฌ์กฐ
์ฝ๋
Application.java
package lotto;
public class Application {
public static void main(String[] args) {
CalculateResult calculateResult = new CalculateResult();
calculateResult.getUserInput();
calculateResult.calculate();
calculateResult.printResult();
}
}
User.java
package lotto;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class User {
public int getAmount() {
return amount;
}
public List<Integer> getAnswer() {
return answer;
}
public int amount;
public List<Integer> answer = new ArrayList<>();
void inputAmount(){
System.out.println("๊ตฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.");
Scanner sc = new Scanner(System.in);
int inputAmount = sc.nextInt();
inputAmount /= 1000;
this.amount = inputAmount;
}
void inputAnswer(){
System.out.println("๋น์ฒจ ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.");
Scanner sc = new Scanner(System.in);
String[] str = sc.nextLine().split(",");
System.out.println(str[0]);
// for (int i = 0; i < 6; i++) {
// answer.add(i, Integer.parseInt(str[i]));
// }
System.out.println("๋ณด๋์ค ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.");
answer.add(sc.nextInt());
}
void printAnswer(){
System.out.println(answer);
}
}
LottoMachine.java
package lotto;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;
public class LottoMachine {
List<Integer> lottoNumber = new ArrayList<>();
List<List<Integer>> lottoNumbers = new ArrayList<>();
public List<Integer> getAnswer() {
return answer;
}
List<Integer> answer = new ArrayList<>();
public int amount;
public List<List<Integer>> getLottoNumbers() {
return lottoNumbers;
}
void getUser(){
User user = new User();
user.inputAmount();
this.amount = user.getAmount();
user.inputAnswer();
user.printAnswer();
this.answer = user.getAnswer();
}
void generateLotto(){
for (int i = 0; i < amount; i++) {
lottoNumber = Randoms.pickUniqueNumbersInRange(1, 10, 6);
lottoNumbers.add(lottoNumber);
System.out.println(lottoNumbers.get(i));
}
}
}
CalculateResult.java
package lotto;
import java.util.ArrayList;
import java.util.List;
public class CalculateResult {
List<List<Integer>> lottoNumbers = new ArrayList<>();
public List<Integer> answer = new ArrayList<>();
//๊ฒฐ๊ณผ ๊ฐ ์ ์ฅ์ ์ํ ๋ณ์
int[] result = {0,0,0,0,0};
double rate;
void getUserInput(){
LottoMachine lottoMachine = new LottoMachine();
lottoMachine.getUser();
lottoMachine.generateLotto();
this.lottoNumbers = lottoMachine.getLottoNumbers();
System.out.println(" ============== ");
this.answer = lottoMachine.getAnswer();
}
void calculate(){
List<Integer> number = new ArrayList<>();
int count;
for(int i = 0; i < lottoNumbers.size(); i++) {
number = lottoNumbers.get(i);
count = 0;
for(int j=0; j < number.size(); j++){
//๋ณด๋์ค ๋ฒํธ ๊ณ์ฐ
if(j==6 && count==4) result[3]++;
else if(answer.contains(number.get(j))) count++;
}
System.out.print(count);
if(count == 3) result[0]++;
else if(count == 4) result[1]++;
else if(count == 5) result[2]++;
else if(count == 6) result[4]++;
}
rate = 5000*result[0] + 50000*result[1] + 1500000*result[2] + 30000000*result[3] + 2000000000*result[4];
rate /= lottoNumbers.size() * 1000 * 100;
}
void printResult(){
System.out.println("\n๋น์ฒจ ํต๊ณ\n---");
System.out.println("3๊ฐ ์ผ์น (5,000์) - " + result[0] +"๊ฐ");
System.out.println("4๊ฐ ์ผ์น (50,000์) - " + result[1] +"๊ฐ");
System.out.println("5๊ฐ ์ผ์น (1,500,000์) - " + result[2] +"๊ฐ");
System.out.println("5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น (30,000,000์) - " + result[3] +"๊ฐ");
System.out.println("6๊ฐ ์ผ์น (2,000,000,000์) - " + result[4] +"๊ฐ");
System.out.printf("์ด ์์ต๋ฅ ์" + "%.1f" +" %% ์
๋๋ค.", rate);
}
}
'โก๐ฉโ๐ป โก > ยบ Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์คํธ๋ฆผ(Stream) (0) | 2023.11.29 |
---|---|
[Java] int์ Integer์ ์ฐจ์ด (0) | 2023.11.29 |
[Java] ๋๋์ ํ๊ณ ์์์ ์ฌ๋ฆผํ๊ธฐ | ์์์ ์ฌ๋ฆผ - Math.ceil (0) | 2023.11.29 |
[์์ผ ํ๋ก๊ทธ๋๋ฐ] ์คํธ๋ฆผ์ ์ด์ฉํ ์ฑํ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.11.28 |
[Java ์ปฌ๋ ์ ํ๋ ์์ํฌ] HashMap (0) | 2023.11.27 |