독개

로또번호 6개 다른값뽑기

by #독개#
선생님꺼

package hacker;


import java.time.LocalDate;

import java.util.ArrayList;

import java.util.Random;

public class Main {


public static boolean search(int start , ArrayList<Integer> lotto){

int size = lotto.size();

for(int ct= 0; ct<size; ct++) {

if(start == lotto.get(ct)) {

return true;

}

}

return false;

}

public static void main(String[] args) {

System.out.print(LocalDate.now().toString() + " 로또번호 : ");

Random rand = new Random();

int value = 0;

ArrayList<Integer> lotto = new ArrayList<Integer>();

//로또번호 6개추출

for(int ct = 0 ; ct<6;ct++) {

value = rand.nextInt(45) + 1;

while(Main.search(value, lotto)) {

value=rand.nextInt(45) + 1;

}

lotto.add(value);

}

for(int a : lotto) {

System.out.print(a + " ");

}

}

}


내꺼



package hacker;


import java.time.LocalDate;

import java.util.ArrayList;

import java.util.Random;


public class Main {


public static void main(String[] args) {

Random random = new Random();

int value ;

boolean a = true ;

ArrayList<Integer> list = new ArrayList<Integer>() ;

value = (random.nextInt(45)) + 1 ;

list.add(value);

for(int ct=0 ; ct < 5; ct++)

{

value = (random.nextInt(45)) + 1 ;

for(int ct2=0 ; ct2<list.size() ; ct2++)

{

a=false;

if (list.get(ct2) == value)

{

ct--;

a=true;

break;

}

}

if(!a)

{

list.add(value);

}

}

for(int c : list)

{

System.out.print(c + " ");

}

}

}



블로그의 정보

독한 개발자

#독개#

활동하기