Develop/곤부📙

삼일절 혼공 / (로또 배열 복습,수동입력 중복제거 해결!!)

밀김 2023. 3. 1. 11:42
728x90

 

 

 

(로또 배열 어려워서 어제자 코드 그냥 복습한거!)

 

		int[] lotto = new int[6];//로또 배열 생성
		Random rand = new Random();//랜덤 객체 생성
	
		//로또 중복 제거
		for(int i=0; i<lotto.length; i++) {
			//인덱스 i
			lotto[i] = rand.nextInt(45)+1;
			//로또 배열에 랜덤 숫자 먼저 담아줌
			for(int j=0; j<i; j++) {
				//인덱스 i와 비교대상인 j
				if(lotto[i]==lotto[j]) {
				//++를 해주면 0이 나옴 그래서 --로 해줘야함 음수는 범위에 없으니까
					i--;
				}
			}
		}
	
			
		

		//로또 정렬하기(=i와 j 값을 비교해서 자리 바꿔주기)
		for(int i=0; i<lotto.length-1; i++) {
			//-1하는 이유는 비교 횟수가 총 다섯번이기 때문에
			for(int j=0; j<lotto.length-1-i; j++) {
				//-1은 위에서 그대로 내려오고,-i은 이미 비교한 인덱스를 또 비교하면 안되니까
				if(lotto[i]>lotto[j]) {
					//앞자리가[i] 뒷자리수[j]보다 크다면 자리를 바꿔라 > 큰수가 뒤로 가게끔
					int tmp = lotto[i];
					lotto[i] = lotto[j];
					lotto[j] = tmp;
				}
			}
		}
			
				
		
	
		//로또 출력하기
		for(int i=0; i<lotto.length; i++) {
			System.out.printf("%d ",lotto[i]);
		}

 

 

 

 

< 로또 배열 찐최종 (자동,수동입력 중복제거+정렬+출력) >

package ex3.control;
import java.util.Random;
import java.util.Scanner;
public class homework2 {
	public static void main(String[] args) {
		
		boolean run = true;
		//while문 작동
		
		int[] lotto = new int[6];
		Random rand = new Random();
		
		System.out.println("< Lotto 관리 프로그램 >");
		System.out.print("1.번호 자동 생성\n2.번호 수동 생성\n3.내 로또 번호 보기\n4.종료\n> ");
        
		while(run) {
			Scanner sc = new Scanner(System.in);
			int num = sc.nextInt();
			
			//자동생성
			if(num==1) {
				System.out.println("< Lotto 번호 자동생성 >");
				System.out.println("인덱스 │   로또번호");
	
				for(int i=0; i<lotto.length; i++) {
					lotto[i] = rand.nextInt(45)+1;
					for(int j = 0; j<i; j++) {
						if(lotto[i] == lotto[j]) {
							i--;
							break;
						}
					}
				}//자동 중복제거
				
				int tmp=0;
				for(int i=0; i<lotto.length-1; i++) {
					for(int j=0; j<lotto.length-1-i; j++) {
						if(lotto[j+1]<lotto[j]) {
							tmp = lotto[j];
							lotto[j] = lotto[j+1];
							lotto[j+1] = tmp;
						}
					}
				}//자동 정렬
				
				
				System.out.print("1     │");
				
				for(int i=0; i<lotto.length; i++) {
					System.out.printf("%d ",lotto[i]);
				}
				System.out.print("\n1. 저장하고 메인메뉴로 가기\n2.취소하고 메인메뉴로 가기\n>");
			}//자동 최종출력
			
			
			
			
			
			//수동생성
			else if(num==2) {
				System.out.println("< Lotto 번호 수동생성 >");
				System.out.print("번호를 입력하세요 (예; 6 7 12 20 40 45)\n>");
				
				for(int i=0; i<lotto.length; i++) {
					lotto[i] = sc.nextInt();
				} 
				
				int[] tmp = new int[5]; //중복없는 정상값만 담을 배열
				int count = 0;	//중복없는 정상값만 담을 변수
				
				for(int i=0; i<lotto.length; i++) {
					boolean flag = false; //boolean 기본값은 flase
					for(int j=0; j<tmp.length; j++) {
						if(lotto[i] == tmp[j]) { //중복이 발생하면
							flag = true; //flag에 true를 저장
						}
					}
					
					if(!flag) { //중복이 발생하지 않은 값들은 
						tmp[count++] = lotto[i]; //lotto[i]배열에 남게 되고, lotto배열의 값을 tmp배열에 저장해주는데
						//count++의미는, tmp의 길이가 tmp의[i] 즉, tmp[5]였는데 정상값들이 lotto에 저장되는 횟수만큼 tmp[i]는 유동적으로 변해야하기 때문에
						//count변수를 tmp배열에 넣어서 lotto[i]에 정상값이 저장될때마다 tmp[count++] 카운트도 하나씩 올라가면서 결과적으로 tmp[i]의 길이가 형성된다
						//그럼 tmp길이를 [6]으로 하면 되는거 아니냐? no! 그렇게 하면 중복값이 0으로 나옴(해결방법이 있을거 같긴 한데 지금은 모르겠음)
					}
				}//수동 중복제거
				

				
				for(int i=0; i<tmp.length-1; i++) {
					for(int j=0; j<tmp.length-1-i; j++) {
						if(tmp[i+1]<tmp[i]) {
							int temp = tmp[i];
							tmp[i] = tmp[i+1];
							tmp[i+1] = temp;
					}
					}
				}//수동 정렬
				
				
				for(int i=0; i<tmp.length; i++) {
					System.out.printf("%d ",tmp[i]);
				}//수동 최종출력

				System.out.print("\n1. 저장하고 메인메뉴로 가기\n2.취소하고 메인메뉴로 가기\n>");
			}//로또 끝
		}//end of while
		

		
	}
}

 

 

< 로또 배열 수동입력 중복제거부분만 따로  >

		int[] arr = new int[6];
		//수동 입력값 저장 배열
		Scanner sc = new Scanner(System.in);
		//입력값 스캐너
		

		// 1.입력값 10개를 0 부터 9 인덱스에 저장 받고
		for(int i = 0; i < arr.length; i++){
			arr[i] = sc.nextInt();
		}
		
		
		
		
        // 중복이 배제된 값을 저장받을 배열 temp = 중복이 안된 정상값 어찌됐건 중복은 하나이상이 나오니까 길이는,6에서 1이 빠진 5
		int[] temp = new int[5];
        // 중복이 배제된값 회수하기위한 변수 count
		int count = 0;
		
        // 중복을 걸러주는 부분
		for(int i = 0; i < arr.length; i++){
			boolean flag = false; //boolean 기본값은 flase
			for(int j = 0; j < temp.length; j++){
				if(arr[i] == temp[j]){ //중복을 찾으면
					flag= true; //flag에 true를 대입
				}
			}
	         // 중복이 아닌 정상값일때 
			 if(!flag){ //만약 flag가 flase라면 = 중복이 아니라면
				temp[count++] = arr[i];
				
				}
		}

		
		
		//중복이 제거된 최종 값 출력
		for(int i = 0; i < temp.length; i++){
			System.out.printf("%d ",temp[i]);
		}

 

 

 

 

👇 (이분꺼 보고 해결함..!) 👇

https://velog.io/@sjsrkdgks/%EB%B0%B0%EC%97%B4%EA%B3%BC-for%EB%AC%B8%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EB%AC%B8%EC%A0%9C%EC%A4%91%EB%B3%B5%EC%A0%9C%EA%B1%B0

 

배열과 for문을 이용한 문제(중복제거)

그 동안 우리가 배운 것으로 간단한 문제를 풀어보자.내 블로그에 처음 올리는 문제인데 이 문제는 나도 비전공자로써 많이 어려웠던 문제이다. 하지만 한 시간 고민 끝에 힘들게 문제를 풀었던

velog.io

 

 

 

 

❓ boolean을 사용하는 이유

프로그래밍 언어에서 사용되는 논리 자료형(data type)을 이해하는 데 도움을 주고

또한, 조건문과 반복문 등 제어문의 동작을 이해하는 데도 도움을 줌

 

 

 

 

 

 

 

 

728x90