본문 바로가기

DEMO CODE/JAVA 기초

[JAVA] Ex010 로또번호 출력하기

반응형


public class LottoMain {

public static void main(String[] args) {

Lotto lotto = new Lotto();

lotto.make();

print(lotto.getBall());

}

public static void print(int [] a){

int count=a.length;

System.out.print("로토 번호는 : ["+a[0]+", ");

for(int i=1;i<count-1;i++){

System.out.print(a[i]+", ");

}

System.out.println(a[count-1]+"]");

}

}


class  Lotto {

private int[] ball;

public Lotto(){

ball=new int[6];

}

public int[] getBall(){

return ball;

}

private boolean isSame(int [] a, int b){

boolean isS=false;

for(int i=0;i<a.length;i++){

if(a[i]==b){

isS=true;

break;

}

}

return isS;

}

//원하는 수의 배열 만들기 

public  void make(){

int count=0;

while(true){

// Math.random()  0<=n<1까지 실수

int b=(int)(Math.random()*45)+1; 

if(! isSame(ball,b)){

ball[count++]=b;

}

if(count==6){break;}

}

}

}

반응형

'DEMO CODE > JAVA 기초' 카테고리의 다른 글

[JAVA] Ex009 ReverseArray - 정렬  (0) 2015.05.19
[JAVA] Ex008 BubbleSort - 정렬  (0) 2015.05.19
[JAVA] Ex007 Calendar, Date 정보출력  (0) 2015.05.19
[JAVA] Ex006 다중for문, if 예제  (0) 2015.05.19
[JAVA] Ex005 Math method  (0) 2015.05.19