공부/과제.

JAVA 문제풀이 03

햄이; 2021. 4. 26. 14:56

 

 

Q1. 문자열 변수에 문자열을 넣고 초기화 한 뒤, 랜덤값으로 하나를 추출하여 유저와 일치할 때까지 게임 실행

(단, 문자열에 있는 문자가 아니라면 잘못된 입력임을 명시해주고, 게임 시도 횟수에 포함X

 

 

package Day4;

import java.util.Random;
import java.util.Scanner;

/*
 * Q7.
 * 문자열 변수에 "인천직업능력개발원"으로 초기화된 변수를 생성
 * 그 뒤 랜덤을 이용하여 문자를 하나 추출해 char com에 저장
 * 그리고 유저는 글자를 입력해서 컴퓨터가 가지고 있는 값이랑 일치를 할 때까지 게임 실행
 * 컴퓨터가 가지고 있는 문자와 일치되면 게임은 끝이 난다
 * 게임이 끝나면 컴퓨터가 가지고 있는 문자와 몇번만에 맞춰지는 출력을 해줄 것
 * (단, 인천직업능력 개발원중에 문자가 아니면 잘못된 입력이라고 하고 시도횟수에 포함하지 않습니다.)
 * */
 
public class Question7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String str = "인천직업능력개발원";
		Random r = new Random();
		int i = r.nextInt(9);//0~8까지 랜덤함수
		char com = str.charAt(i); // 컴퓨터에 문자 하나 추출하여 저장
		//char com = str.charAt(r.nextInt(9)); 이렇게도됨
		int cnt = 0;//몇 번 시도했는지 카운트
		
		
		
		while(true){
			System.out.println("'인천직업능력개발원' 중 문자 하나를 입력해주세요");
			Scanner sc = new Scanner(System.in);
			String user = sc.next();//= char user = sc.next().charAt(0);
			int check = 0;//반복 돌릴 때 마다 초기화
			
			for(int j=0; j<9; j++) {
					if(user.charAt(0) == str.charAt(j)) { 
                    //문자열에 문자가 포함되는지 확인
						check += 1; //문자열에 포함될 시 check 표시해줌
						break;//일치할 때 break걸어줌
					}
			}
			if(check==1) { //문자열에 포함이 될 시
				if(user.charAt(0)==com) {
					System.out.println("컴퓨터의 문자 :" + com);
					System.out.println("유저의 문자 :"+ user);
					System.out.println("총 시도 횟수 :"+ cnt);
					System.out.println("컴퓨터의 문자와 일치합니다. 게임을 종료합니다.");
					break;
					
				}else {
					System.out.println("컴퓨터의 문자 :" + com);
					System.out.println("유저의 문자 :"+ user);
					System.out.println("컴퓨터의 문자와 일치하지 않습니다. 게임을 계속합니다.");
					System.out.println();
					cnt += 1;
				
				}
			}else {
				System.out.println("잘못된 입력입니다. 초기화면으로 돌아갑니다.");
			}
			
		}
	}

}

 

아래는 위와 똑같은 문제이지만 

처음에 풀 때 함수를 이용해서 푼 버전이다.

그러나 당시 진도상

원하는 풀이방식이 아니었기에 위에 버전으로 다시 풀었었다ㅎ

이미 푼거 아까워서 같이 업로드!

 

package Day4;

import java.util.Random;
import java.util.Scanner;

// Q7. 함수 사용버전
public class Question1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String str = "인천직업능력개발원";
		Random r = new Random();
		int i = r.nextInt(9);//0~8까지 랜덤함수
		char com = str.charAt(i); // 컴퓨터에 문자 하나 추출하기
		int cnt = 0;

		while(true) {
			System.out.println("'인천직업능력개발원' 중 문자 하나를 입력해주세요");
			Scanner sc = new Scanner(System.in);
			String user = sc.next();


			if(str.contains(user)) { //문자열에 문자가 포함되는지 확인하는 함수

				if(user.equals(Character.toString(com))) { // char를 String으로 변환해줌
					System.out.println("컴퓨터의 문자 :" + com);
					System.out.println("유저의 문자 :"+ user);
					System.out.println("총 시도 :"+ cnt);
					System.out.println("컴퓨터의 문자와 일치합니다. 게임을 종료합니다.");
					break;
				}else {
					System.out.println("컴퓨터의 문자 :" + com);
					System.out.println("유저의 문자 :"+ user);
					System.out.println("컴퓨터의 문자와 일치하지 않습니다. 게임을 계속합니다.");
					cnt += 1;
				}

			}else {
				System.out.println("잘못된 입력입니다. 초기화면으로 돌아갑니다.");
			}


		}


	}

}