java 로 Lotto 만들어 보기
import java.util.Scanner;
public class Ex_04_25_Math_lotto_남동길 {
public static void main(String[] args) {
int[] lotto = new int[6]; // 1~45 중복 안되게 삽입
int[] my = new int[6]; // Scanner 입력
int count=0; // 맞은 갯수
Scanner sc = new Scanner(System.in);
int i,j;
for(i=0; i<lotto.length; i++){
lotto[i] = (int)((Math.random()*45)+1);
if(i>0){
for(j=0; j<i; j++){
while(lotto[i]==lotto[j]){
lotto[i] = (int)((Math.random()*45)+1);
}
}
}
}
for(i=0; i<my.length; i++){
System.out.print("숫자"+(i+1)+" : ");
my[i] = sc.nextInt();
}
System.out.println();
System.out.println("내가 뽑은 숫자 : ");
for(i=0; i<my.length; i++){
System.out.print(my[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("컴이 뽑은 숫자 : ");
for(i=0; i<lotto.length; i++){
System.out.print(lotto[i]+" ");
}
System.out.println();
System.out.println();
for(i=0; i<my.length; i++){
for(j=0; j<lotto.length; j++){
if(my[i] == lotto[j]){
count += 1;
}
}
}
System.out.println("맞은 갯수 : "+count);
switch(count){
case 6: System.out.println("1등 당첨입니다.");
break;
case 5: System.out.println("2등 당첨입니다.");
break;
case 4:System.out.println("3등 당첨입니다.");
break;
case 3:System.out.println("4등 당첨입니다.");
break;
default : System.out.println("꽝~!~!~!");
}
}
}
public class Ex_04_25_Math_lotto_남동길 {
public static void main(String[] args) {
int[] lotto = new int[6]; // 1~45 중복 안되게 삽입
int[] my = new int[6]; // Scanner 입력
int count=0; // 맞은 갯수
Scanner sc = new Scanner(System.in);
int i,j;
for(i=0; i<lotto.length; i++){
lotto[i] = (int)((Math.random()*45)+1);
if(i>0){
for(j=0; j<i; j++){
while(lotto[i]==lotto[j]){
lotto[i] = (int)((Math.random()*45)+1);
}
}
}
}
for(i=0; i<my.length; i++){
System.out.print("숫자"+(i+1)+" : ");
my[i] = sc.nextInt();
}
System.out.println();
System.out.println("내가 뽑은 숫자 : ");
for(i=0; i<my.length; i++){
System.out.print(my[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("컴이 뽑은 숫자 : ");
for(i=0; i<lotto.length; i++){
System.out.print(lotto[i]+" ");
}
System.out.println();
System.out.println();
for(i=0; i<my.length; i++){
for(j=0; j<lotto.length; j++){
if(my[i] == lotto[j]){
count += 1;
}
}
}
System.out.println("맞은 갯수 : "+count);
switch(count){
case 6: System.out.println("1등 당첨입니다.");
break;
case 5: System.out.println("2등 당첨입니다.");
break;
case 4:System.out.println("3등 당첨입니다.");
break;
case 3:System.out.println("4등 당첨입니다.");
break;
default : System.out.println("꽝~!~!~!");
}
}
}
결과
숫자 6개를 입력받아
무작위로 중복되지 않게 생성한 숫자 6개와 비교하여
같은개수에 따라서 등수와 꽝을 출력해주는 프로그램
댓글
댓글 쓰기