국비 6-2

냐아암·2023년 4월 21일
0

국비

목록 보기
10/114

BranchExample

  • do while: while문을 무조건 한 번 실행하고 특정 조건에 종료
  • break: 반복문을 바로 멈춤
  • continue: 다음 반복으로 넘어감
  • String 타입에서 문자열 비교 시 equals 사용
  • String 빈 값:null
  • Math.random(): 0.0 이상, 1.0 미만의 난수 생성
    -사용법: (int)(Math.random() * n +1)
    -->1부터 n까지 난수 생성하기
package edu.kh.control.branch;

import java.util.Scanner;

public class BranchExample {
	
	public void ex1() {
		
		//1부터 10까지 1씩 증가하며 출력하는 반복문 작성
		//단 5를 출력하면 반복문을 멈춤 1 2 3 4 5
		
		for(int i=1;i<=10;i++) {
			System.out.print(i+" ");
			
			if(i==5) {
				break; //반복문을 멈춥니다.
			}
		}
		
		
	}
	public void ex2() {
		
		//0이 입력될 때까지의 모든 정수의 합 구하기
		
		Scanner sc = new Scanner(System.in);
		
		int input=0;
		int sum=0;
		
		//while문의 처음에 무조건 수행하고, 특정 조건에 종료하는 방법
		//1) input에 초기값을 0이 아닌 다른 값
		//2) do while사용
		//3) 무한루프 상태의 while문을 만들고 내부에 break조건 작성

		while(true) { //무한루프
			System.out.print("정수 입력: ");
			input=sc.nextInt();
			
			sum+=input;
			
			//입력 받은 수가 0인지 검사(while 종료 조건)
			if (input ==0) {
				break;
			}
			
		}
		System.out.println("합계: " +sum); //Unreachable code
		
	}
	public void ex3() {
		
		//입력받은 모든 문자열을 누적
		//단, "exit@" 입력 시 문자열 누적을 종료하고 결과 출력
		
		Scanner sc = new Scanner(System.in);
		
		String str = ""; //쌍따옴표("")를 이용해 String리터럴임을 지정
		
		while (true) { //무한루프
			
			System.out.print("문자열 입력(exit@입력 시 종료): ");
			String input = sc.nextLine();
			//next(); 한 단어 (띄어쓰기 포함 x)
			//nextLine(); 한 문장 (띄어쓰기 포함)
			
			//입력 받은 문자열이 "exit@"이면 반복 종료
			
//			if (input=="exit@") {
			if (input.equals("exit@")) {
				//String 자료형은 비교 연산자(==)로 같은 문자열인지 판별할 수 없다.
				
				//비교 연산자는 보통 기본 자료형끼리의 연산에만 사용가능하다.
				//Stirng은 기본 자료형이 아니라 참조형
				//-> 해결법 : 문자열1.equals(문자열2)로 비교 가능하다.
				break;
			}
		
			str += input +"\n" ; //누적 시 개행문자 추가
		}
		System.out.println("===========================");
		System.out.println(str);
		
		
	}
public void ex4() {
		
		//중첩 반복문 내부에서 break 사용하기
		
		//구구단을 2~9단까지 모두 출력
		//단 2단은 X2까지, 3단은 X3까지 ..... 9단은 X9까지 출력
		
		for (int dan =2; dan<=9; dan++) {
			for (int k=1; k<=9; k++) {
				
				System.out.printf("%d X %d = %2d  ", dan, k,dan*k);
				
				if(k==dan) {
					break;
					//분기문이 중첩 반복문 내에서 사용되면 가장 가까운 반복문에 작용
				}
			}
			System.out.println();
		}
	}
	public void ex5() {
		
		//break: 반복문을 바로 멈춤
		//continue: 다음 반복으로 넘어감
		//1~10까지 1씩 증가하며 출력 1 2 4 5 7 8 10 
		//단, 3의 배수는 제외
		
		for(int i=1; i<=10; i++) {
			if (i%3==0) {
				continue;
			}
			System.out.print(i + " ");
		}

	}
	public void ex6() {
		
		//1~100까지 1씩 증가하며 출력하는 반복문
		//단 5의 배수는 건너뛰고 증가하는 값이 40이 되었을 때 반복을 멈춤 
		
		for(int i=1; i<=100; i++) {
			if (i==40) {
				break;
			}
			if (i%5==0) {
				continue;
			}
			System.out.print(i+ " ");
		}
		
	}
	public void RPSGame() { //equals, Math.random(), String null로 초기화, boolean 사용
		// 가위 바위 보 게임    ctrl shift F -> 줄맞춤

		// 몇판? : 3

		// 1번째 게임
		// 가위/바위/보 중 하나를 입력 해주세요 : 가위
		// 컴퓨터는 [보]를 선택했습니다.
		// 플레이어 승!
		// 현재 기록 : 1승 0무 0패

		// 2번째 게임
		// 가위/바위/보 중 하나를 입력 해주세요 : 보
		// 컴퓨터는 [보]를 선택했습니다.
		// 비겼습니다.
		// 현재 기록 : 1승 1무 0패

		// 3번째 게임
		// 가위/바위/보 중 하나를 입력 해주세요 : 가위
		// 컴퓨터는 [바위]를 선택했습니다.
		// 졌습니다ㅠㅠ
		// 현재 기록 : 1승 1무 1패
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("가위바위보 게임");
		System.out.print("몇 판? :");
		
		int round = sc.nextInt();
		
		//승패 기록용 변수
		int win = 0;
		int draw = 0;
		int lose = 0;
		
		
		for (int i=1; i<=round; i++ ) { //입력받은 판 수만큼 게임
			
			System.out.println("\n"+i+" 번째 게임");
			
			System.out.print("가위/바위/보 중 하나를 입력해주세요: " );
			String input=sc.next(); //플레이어가 가위/바위/보 입력
			
			//컴퓨터 가위/바위/보 정하기(랜덤)
			//1) 1~3 사이 난수 생성
			//2) 1이면 가위, 2이면 바위, 3이면 보 지정 (switch)
			
			//난수 생성 방법: Math.random()
			// -> 0.0이상 1.0 미만의 난수 생성
			
			int random=(int)(Math.random() * 3 + 1);
			// 0.0 <= x <1.0
			// 0.0 <= x*3  <3.0
			// 1.0 <= x*3 +1  <4.0
			// 1 <= (int)(x*3 +1) <4 
			// 1 2 3 (4미만의 정수)
			
			String com = null; //컴퓨터가 선택한 가위/바위/보 저장하는 변수
			//null: 아무것도 참조하고 있지 않음
			
			switch(random) {
			case 1 : com="가위"; break;
			case 2 : com="바위"; break;
			case 3 : com="보"; break;
			
			
			}
			System.out.printf("컴퓨터는 [%s]를 선택했습니다.\n", com);
			//The local variable com may not have been initialized -> null로 초기화
			//null: 아무것도 참조하고 있지 않음
			
			//컴퓨터와 플레이어(기준) 가위 바위 보 승자 판별
			//win, draw, lose
			
			
			//String 비교 시 equals() 사용
			if (input.equals(com)) { //비긴경우
				System.out.println("비겼습니다.");
				draw++;
			}else {
				
				boolean win1 = input.equals("가위") && com.equals("보");
				boolean win2 = input.equals("바위") && com.equals("가위");
				boolean win3 = input.equals("보") && com.equals("바위");
				
				if (win1 || win2 || win3) { //내가 이겼을 때
					System.out.println("플레이어 승 !");
					win++;
				}else {
					System.out.println("졌습니다ㅜㅜ");
					lose++;
				}
				
				/*
				if (input.equals("가위") && com.equals("보")) {
					System.out.println("플레이어 승 !");

				} else if (input.equals("바위") && com.equals("가위")) {
					System.out.println("플레이어 승 !");

				} else if (input.equals("보") && com.equals("바위")) {
					System.out.println("플레이어 승 !");

				} else {
					System.out.println("졌습니다ㅜㅜ");
				}
				*/
			}//else 끝
			System.out.printf("현재 기록: %d승 %d무 %d패\n", win, draw, lose);
			
			
		}
		
		
	}

}

BranchRun

package edu.kh.control.branch;

public class BranchRun {

	public static void main(String[] args) {
		
		BranchExample branchEx = new BranchExample();
		
		branchEx.ex1();
		branchEx.ex2();
		branchEx.ex3();
		branchEx.ex4();
		branchEx.ex5();
		branchEx.ex6();
		branchEx.RPSGame();

	}

}
profile
개발 일지

0개의 댓글