Math.random() 메소드를 사용하면 랜덤숫자를 얻을 수 있습니다.
하지만, 중복된 값을 얻을 수도 있습니다.
HashSet을 사용하면 중복된 데이터 없는 랜덤숫자를 얻을 수 있습니다.
public static void main(String[] args) {
HashSet<String> lottoSet = new HashSet<String>();
ArrayList<String> arrList = new ArrayList<String>();
while(lottoSet.size() < 7){
int num = (int)(Math.random()*45);
//0이 아닐때만 넣는다.
if(num != 0){
lottoSet.add(num+"");
}
}
//set을 list에 담기
arrList.addAll(lottoSet);
for(int i=0; i < arrList.size(); i++){
if(i == 6) {
System.out.print(arrList.get(i));
}else {
System.out.print(arrList.get(i)+",");
}
}
}
}
[JAVA] 여러개의 랜덤숫자 생성과 최대값 최소값 구하기 (0) | 2020.11.14 |
---|---|
[JAVA] ArrayList 중복데이터 제거방법 (0) | 2020.06.26 |
[자바] double 지수형태 알파벳 숫자를 원래 숫자로 바꾸는 방법[BigDecimal] (0) | 2020.05.25 |
[자바] 오라클 JDBC 연결 (0) | 2018.01.17 |
[JAVA] ArrayList 긴 문자열 데이터 잘라서 담기 substring(), add() (0) | 2017.11.25 |
댓글 영역