과제1. 사용자의 값 난수, 컴퓨터의 값 난수를 발생, 정렬을 사용하여 로또를 돌려보시오.
(보너스 번호 추가시 가산점)
==============================소스================================
package example;
import java.util.Random;
public class lotto
{
public static void main(String[] args)
{
int intArray[] = new int[6];
int intArray2[] = new int[6];
int temp;
int count=0;
int bonus = 0;
int not = 0;
Random rand = new Random();
bonus = (int)(rand.nextInt(45)+1);
for(int i=0;i<6; i++) /*컴퓨터 로또생성*/
{
intArray2[i] = (int)(rand.nextInt(45)+1);
for(int j=0;j<i;j++){
if(intArray2[j] == intArray2[i])
{
i--;
break;
}
}
}
for(int i=0; i<6; i++){
if(bonus==intArray2[i]){
bonus = (int)(rand.nextInt(45)+1); /*보너스랑 컴퓨터 로또랑 숫자 겹치면 난수 재생성*/
}
}
for(int i=0;i<6; i++) /*내꺼 로또생성*/
{
intArray[i] = (int)(rand.nextInt(45)+1);
for(int j=0;j<i;j++){
if(intArray[j] == intArray[i])
{
i--;
break;
}
}
}
for(int i=0; i<5; i++){ /*컴퓨터 로또 버블정렬*/
for(int j=0; j<6-(i+1); j++){
if(intArray2[j]>intArray2[j+1]){
temp = intArray2[j+1];
intArray2[j+1] = intArray2[j];
intArray2[j] = temp;
}
}
}
for(int i=0; i<5; i++){ /*내꺼 로또 버블정렬*/
for(int j=0; j<6-(i+1); j++){
if(intArray[j]>intArray[j+1]){
temp = intArray[j+1];
intArray[j+1] = intArray[j];
intArray[j] = temp;
}
}
}
for(int i=0; i<6; i++){ /*컴퓨터 로또랑 내꺼 로또 비교*/
for(int j=0; j<6; j++){
if(intArray[i] == intArray2[j]){
count = count +1;
}
}
}
for(int i=0;i<6;i++){
System.out.print(""+intArray2[i]+"\t");
}
System.out.println("bonus : "+bonus);
for(int i=0;i<6;i++){
System.out.print(""+intArray[i]+"\t");
}
System.out.println("count : "+count);
switch(count){
case 2:
System.out.println("6등입니다.");
break;
case 3:
System.out.println("5등입니다.");
break;
case 4:
System.out.println("4등입니다.");
break;
case 5:
for(int i=0; i<6; i++){ /*bonus랑 같은지 6번 검사해야됨*/
if(count==5 && intArray[i] == bonus){ /*맞은 개수가 5개이고 , 보너스 까지 같은게 있으면 2등*/
System.out.println("2등입니다.");
}
else if(intArray[i] != bonus){
not = not+1; /*같지 않는거 갯수셈*/
}
}
if(not==6){ /*case 5 (and) not==6 (컴퓨터랑 로또 5개 맞고 , 내 6개중에 bonus랑 같은게 하나도 없음.) 즉 3등.*/
System.out.println("3등입니다.");
}
break;
case 6:
System.out.println("1등입니다.");
break;
default:
System.out.println("꽝입니다.");
}
}
}
for문으로 떡칠했지만 성취감 ㅅㅌㅊ
혹시 뭐 고쳐야될거 있냐??
순서대로 하려면 포문 떡칠될 수 밖에 없지않나
72점.