귀차니즘의 압박..ㅎㅎ


~cpp
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;

public class Test001 {
	public static void main(String[] args) {
		
		Random NumberCreator = new Random();
		int[] answers = new int[3];
		int[] inputNumbers = new int[3];
		int strike = 0;
		int ball = 0;

		//// 답을 만듭니다. ////
		for (int i = 0; i < 3; ++i)
		{
			answers[i] = NumberCreator.nextInt(10);
			for (int j = 0; j < i; ++j)
			{
				if (answers[i] == answers[j])
				{
					answers[i] = NumberCreator.nextInt(10);
					j = 0;
				}
			}
		}
		
		while (3 != strike)
		{
			strike = 0;
			ball = 0;
			//// 사용자의 입력을 받습니다. ////
			Scanner NumberScanner = new Scanner(System.in);	
			for (int i = 0; i < 3; ++i)
			{
				inputNumbers[i] = NumberScanner.nextInt();	
			}
			//// 사용자 입력 체크 ////
			boolean  haveError = false;
			for (int i = 0; i < 3; ++i)
			{
				for (int j = 0; j < i; ++j)
				{
					if (inputNumbers[i] == inputNumbers[j])
						haveError = true;
				}
			}
			//// 에러 체크 ////
			if (true == haveError)
			{
				System.out.println("동일한 숫자를 입력하였습니다. 다시 입력해주세요.");
			}
			else
			{
				//// 점수판정 ////
				for (int i = 0; i < 3; ++i)
				{
					if (answers[i] == inputNumbers[i])
						++strike;
					else
					{
						for (int j = 0; j < 3; ++j)
						{
							if (answers[i] == inputNumbers[j])
								++ball;
						}
					}
				}
				
				//// 점수출력 ////
				System.out.print("Strike = ");
				System.out.println(strike);
				System.out.print("Ball = ");
				System.out.println(ball);
			}
		}
	}
}
Retrieved from http://wiki.zeropage.org/wiki.php/BasicJAVA2005/실습1/조현태
last modified 2021-02-07 05:22:34