My footsteps

code 실습 / 1️⃣8️⃣ 본문

국비수업/CODE

code 실습 / 1️⃣8️⃣

밀김 2023. 3. 13. 14:50
728x90

 

 

 

 

 

 

 

 

로또 시뮬레이터를 수업때 배운

static 메서드를 활용하여 각 class에게 객체 넘기기!

 

 

package doyounss;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
public class Lottosimulater {
	public static void main(String[] args) {
		
//		Lotto[] lottos = new Lotto[6];
//		int index=0;
		
		Lottolist list = new Lottolist();
		//Lottolist에 접근할수 있게 객체 생성

		Lottolist.initLottoList(list);
		//출처를 써야한다 Lottolist.
		
		while(true) {
			
			System.out.println("현재 로또 번호: \n1.번호를 다시 뽑기2.마지막 번호를 다시 뽑기");
			
			
			Scanner sc = new Scanner(System.in);
			int menu = sc.nextInt();
			
			switch(menu) {
			case 1:
				Lottolist.setLottoNum(list);
				
				break;
			case 2:
				Lottolist.resetLottoNum(list);
					//1번을 갔다가 2번을 갔을때 작동됨
				break;
			}//스위치
			
			//출력을 위한 for문
			Lottolist.printLottoNum(list);

		}//while
	}//main
}//main class

 

 

package doyounss;

public class Lotto {
	int num;
}

 

 

package doyounss;

import java.util.Random;

public class Lottolist {
	//class에 한데 묶는 이유=유지보수에 용이하게 하려고
	
	Lotto[] lottos;
	int index;

	static void initLottoList(Lottolist list) {
		list.lottos = new Lotto[6];
		list.index=0;
	}
	

	public static void setLottoNum(Lottolist list) {
		Random rand = new Random();
		Lotto lotto = new Lotto();//임시 로또 객체를 만들어
		lotto.num = rand.nextInt(45)+1; //num에접근
		list.lottos[list.index]=lotto;
//		list.lottos[list.index].num=rand.nextInt(45)+1;
		list.index++;
	}
	

	static void resetLottoNum(Lottolist list) {
		Random rand = new Random();
		Lotto lotto = new Lotto();
		if(list.index==0)
			System.out.println("로또 번호가 없습니다");
		else {
			lotto.num = rand.nextInt(45)+1;
			list.lottos[list.index-1]=lotto;
			//else두줄 이상이면 괄호 안에넣어줘야함
			//다시 돌아가서 바꿔야 하니까 -1을 넣어줘야함 
		}
	}


	public static void printLottoNum(Lottolist list) {
		for (int i = 0; i < 6; i++) {
			if(i<list.index) {
				System.out.printf("[%d]",list.lottos[i].num);
			}
			else
				System.out.print("[ ]");
		}
		System.out.println();
	}

}

 

 

 

<함수화 하기전 원본 코드>

//         public static void main(String[] args) {
//            Scanner scan = new Scanner (System.in);
//            Random rand = new Random();
//            Lotto[] lottos = new Lotto[6]; // 우리는 객체 배열로 만들꺼다, 이름만 생김
//            int index=-1;
//            나갈래:
//               while(true) {
//                  int menu;
//                  if(index==5){// 마지막 로또번호가 채워졌을때 종료 하거나 마지막번호 다시뽑기
//                     System.out.println("1.종료 2.마지막 번호를 다시 뽑기");
//                     System.out.print(">");
//                     menu = scan.nextInt();   
//                     if(menu==1)
//                        break 나갈래;
//                  }
//                  else {
//                     System.out.println("현재 로또 번호 :");
//                     System.out.println("1.번호를 뽑기 2.마지막 번호를 다시 뽑기");
//                     System.out.print(">");
//                     menu = scan.nextInt();   
//                  }
//                  Lotto lotto = new Lotto();
//                  switch (menu) {
//                  case 1: //번호를 뽑기
//                     index++;
//                     lotto.num = rand.nextInt(45)+1;//
//                     lottos[index] = lotto;
//                     break;
//                  case 2:
//                     if(index==-1)
//                        System.out.println("로또번호가 없습니다.");
//                     else
//                        lotto.num = rand.nextInt(45)+1;//
//                        lottos[index] = lotto;
//                     break;
//                  } // 스위치
//                  for(int i=0;i<lottos.length; i++) {//
//                     // index = 0; i = 0 -> [%d]
//                     // index = 1; i = 0 -> [%d],
//                     //           i = 1 -> [%d]
//                     if(i<=index) {
//                        System.out.printf("[%d]",lottos[i].num);
//                     }
//                     else
//                        System.out.printf("[ ]");
//                  }
//                  System.out.println();
//                  
//                  printlotto(lottos);
//               } //while문
//            System.out.println("프로그램이 종료되었습니다.");
//         }

 

 

 

1. 함수화 없이 코드를 짠다

2. 함수로 떼서 깔끔하게 만들 부분들은 다 뗀다

3. 뗀것들은 한 class에 몰아넣어서 관리하고 수정한다

 

 


 

(오목과제..하지도못함ㅎ)

 

https://alalstjr.github.io/java/2019/05/01/JAVA-%EC%8A%A4%EC%8A%A4%EB%A1%9C-%EA%B3%BC%EC%A0%9C-%EC%98%A4%EB%AA%A9%EB%A7%8C%EB%93%A4%EA%B8%B0/

 

JAVA 스스로 과제 오목만들기

You may find interesting: 인텔리제이 Lombok 셋팅 인텔리제이 Lombok 셋팅 Posted by 쭌프로 on September 18, 2019 Spring Security 공부 Spring Security 공부노트 Posted by 쭌프로 on July 17, 2019

alalstjr.github.io

 

 

 

⭐️ OmokSimulator.java를 객체지향으로 구현해주세요.

  1. 조에서 둘 씩 짝 지어서 페어로 진행 해주세요. (경험자 + 미경험자)
    경험자가 미경험자 팀원에게 설명 + 지시를 내리고, 미경험자가 설명을 이해하며 코드를 작성해주세요
  2. 설계 그림부터 조원과 함께 그려보세요. 다른 조와 의견을 나눠보세요

 

  1. 출력과 사용자 입력은 다음과 같습니다.

 

[ 턴 1 흑돌 입력 ]

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

> 3 4

 

[ 턴 2 백돌 입력 ] 

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼●┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

> 5 5



[ 턴 3 흑돌 입력 ] 

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼●┼┼┼┼┼┼┼

┼┼┼┼○┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

> 5 5

 

[ 이미 돌이 있습니다. 다시 입력해주세요. ] 

[ 턴 3 흑돌 입력 ] 

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼●┼┼┼┼┼┼┼

┼┼┼┼○┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

┼┼┼┼┼┼┼┼┼┼

 

 

package saturday;

import java.util.Scanner;

public class Omok {
	public static void main(String[] args) {
		
	    String[][] omoks = new String[10][10];
	    Scanner scan = new Scanner(System.in);
	    
	    int index=1;
	    String name1 = "흑";
	    String name2 = "백";

	    for(int i=0;i<10;i++) { //행
	    	for(int j=0;j<10;j++) { //열
	    		omoks[i][j] = "┼";
	    		System.out.print(omoks[i][j]);//바둑판 출력
	    	}
	    	System.out.println();
	    }
	    System.out.println("★오목 게임 시작!★");
	    
	    while(true) {
	    	
	    	if(index%2==1) {
	    		System.out.printf("[턴 %d %s돌 입력]\n",index,name1);
	    	}else
	    		System.out.printf("[턴 %d %s돌 입력]\n",index,name2);
	    	
	    	
			    System.out.print(">");
			    int num = scan.nextInt();
			    int num2 = scan.nextInt();
			    //좌푯값을 입력받을 넘.

			    
//			    if(index%2==1 && omoks[num][num2].equals("╉")) {
//			    	omoks[num][num2]= "●";
//			    }else if(index%2==0 && omoks[num][num2].equals("╉")) {
//			    	omoks[num][num2]= "○";
//			    }else {
//			    	for(int i=0;i<10;i++) {
//			    		for(int j=0;j<10;j++) {
//			    			omoks[i][j]=omoks[i][j];
//			    		}
//			    	}
//			    	System.out.println("이미 놓여진 자리입니다.");
//			    	index--;
//			    }
//			    ▽수정본
			    
			    if(omoks[num][num2].equals("┼")) {
			    	if(index%2==1) {
			    		omoks[num][num2]= "●";
			    	}else {
			    		omoks[num][num2]= "○";
			    	}
			    	index++;
			    }else
			    	System.out.println("이미 놓여진 자리입니다.");
			    
			    
			    for(int i=0;i<10;i++) { //행
				       for(int j=0;j<10;j++) { //열
				    	   System.out.print(omoks[i][j]);
				       }
				       System.out.println();
				 }
			
	    }//while
	   
	}

}

 

 

 

 

 

 

728x90

'국비수업 > CODE' 카테고리의 다른 글

code 실습 / 2️⃣0️⃣  (0) 2023.03.15
code 실습 / 1️⃣9️⃣  (0) 2023.03.14
code 실습 / 1️⃣7️⃣  (0) 2023.03.10
code 실습 / 1️⃣6️⃣  (0) 2023.03.09
code 실습 / 1️⃣5️⃣  (0) 2023.03.08