가상의 로또를 해봅시다!

90 views
Skip to first unread message

문성민

unread,
Apr 23, 2012, 2:32:45 AM4/23/12
to kor...@googlegroups.com
로또가게에 가서 돈을 내면 로또가 만들어지고

실제 로또번호와 비교해서 맞은 개수를 출력해봅시다.

단, 난수는 Math.random()을 이용하고 난수 발생은 같은 숫자가 나오면 안됩니다.

입력
얼마?(원)
4000
------------------------------
출력
자신의 번호
1 2 3 4 5 6
7 8 9 10 11 12

당첨번호
10 11 12 13 14 15

맞은개수
3

Message has been deleted

문성민

unread,
May 16, 2012, 6:18:19 AM5/16/12
to kor...@googlegroups.com
package kr.ac.busanit;

import java.util.ArrayList;

public class Buyer { //구매자 클래스

private Long buyerMoney;

public Buyer(Long buyerMoney){
this.buyerMoney= buyerMoney;
}

public Long getBuyerMoney(){
return buyerMoney;
}

public ArrayList<Integer> buyLotto(Lotto lot){
this.buyerMoney-= lot.getLottoPrice();
return lot.makeLotto();
}
}
package kr.ac.busanit;

import java.util.ArrayList;
import java.util.Random;

public class Lotto { //로또 클래스
final private Long lottoPrice;
public Lotto(){
this.lottoPrice= 1000L;
}
public Long getLottoPrice(){
return lottoPrice;
}
public ArrayList<Integer> makeLotto(){
ArrayList<Integer> number= new ArrayList<Integer>();
ArrayList<Integer> selectNumber= new ArrayList<Integer>();
Integer max= 45;
for(int i= 0; i< max; i++){
number.add(i+ 1); //로또는 1~45까지 이다.
}
for(int i= 0; i< 6; i++){
Integer random=(new Random()).nextInt(number.size()); //0~ 44까지 난수 생성
selectNumber.add(number.get((int)random)); //0~ 44중 나온수를 로또 종이에 출력
number.remove((int)random); //0~ 44중 나온수는 제외,인덱스 값을 가져올려면 인트형이어야 한다.
}
return selectNumber;
}
}
 package kr.ac.busanit;

import java.util.ArrayList;
import java.util.Scanner;

public class LottoMain { //로또가 돌아가는 클래스

public static void main(String[] args) {
ArrayList<Integer> wow= new ArrayList<Integer>(); //당첨 번호
ArrayList<ArrayList<Integer>> paper= new ArrayList<ArrayList<Integer>>(); //로또가 출력되는 용지
Scanner sc= new Scanner(System.in);
Lotto lot= new Lotto(); //로또 생성
wow= lot.makeLotto(); //이번주 번호 생성
System.out.println("이번주 번호:"+ wow);
System.out.println("================================");
System.out.print("당신은 얼마를 가지고 있으십니까?");
Buyer buyer= new Buyer(sc.nextLong()); //814만번 돌려볼려고 long형을 받았다.
while(true){
int five= 0; int four= 0; int three= 0; int one= 0;
System.out.println("당신은 "+ buyer.getBuyerMoney()+"원을 가지고 있으며 "+ (long)buyer.getBuyerMoney()/lot.getLottoPrice()+ "번 로또 가능하십니다.");
System.out.print("로또를 몇번 하시겠어요?"); int chance= sc.nextInt();
if((chance* lot.getLottoPrice())> buyer.getBuyerMoney()){
System.out.println("돈이 부족합니다.  다시 입력 하세요");
}else{
for(int i= 0; i<chance; i++){
paper.add(buyer.buyLotto(lot));
}
}
for(int i= 0; i< paper.size(); i++){
int collect= 0;
for(int j=0; j< paper.get(i).size(); j++){
for(int k= 0; k<wow.size(); k++){
if(paper.get(i).get(j).equals(wow.get(k)))
collect++; //로또번호가 몇개 맞는지 확인
}
}
System.out.println((i+1)+ ".당신의 번호"+ paper.get(i)+ "☞맞은갯수 :"+ collect);
switch(collect){
case 3: System.out.println("★5등★"); five++; break;
case 4: System.out.println("☆4등☆"); four++; break;
case 5: System.out.println("▽3등▽"); three++;break;
case 6: System.out.println("♣1등♣"); one++; break;
}
}
System.out.println("결과: 1등 "+ one+ "번");
System.out.println("결과: 3등 "+ three+ "번");
System.out.println("결과: 4등 "+ four+ "번");
System.out.println("결과: 5등 "+ five+ "번");
System.out.println("현재 잔액 :"+ buyer.getBuyerMoney());
paper.clear();
if(buyer.getBuyerMoney()< lot.getLottoPrice()){
System.out.println("돈이 부족하여 종료합니다.");
break;
}
System.out.println("이번주 번호:"+ wow);
System.out.println("================================");
}
}
}
Reply all
Reply to author
Forward
0 new messages