소스코드

package superCode;

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

//연산자 기호 맞추기 게임 3 ? 4 = 12
//	[문제]
//		1. 10 ~ 20 사이의 랜덤 숫자 2개를 x, y 변수에 각각 저장한다.
//			예) x = 3, y = 4
//		2. 1 ~ 3 사이의 랜덤 숫자 1개를 op 변수에 저장한다.
//			예) op = 3
//		3. op의 값은 연산자 기호에 해당된다.
//			1. +   2. -   3. *
//			  예) op의 값이 3이면 곱하기이다.
//		4. 화면에 숫자 2개와 답을 출력한다.
//		      단, 기호는 출력하면 안된다.
//		      예) 3 ? 4 = 12
//		      	1. +   2. -   3. *
//		5. 사용자는 기호를 맞추면 된다.
//			예) 3 ==> 정답

public class BrainWars {

	public static void main(String[] args) {
		Random ran = new Random();
		Scanner sc = new Scanner(System.in);
		
		// 1. 10 ~ 20 사이의 랜덤 숫자 2개를 x, y 변수에 각각 저장
		int x = ran.nextInt(11) + 10;
		int y = ran.nextInt(11) + 10;
//		System.out.println("x = " + x);
//		System.out.println("y = " + y);
		
		// 2. 1 ~ 3 숫자 1개 저장
		int op = ran.nextInt(3) + 1;
//		System.out.println("op = " + op);
		
		// 3. x와 y를 op변수에 맞게 계산한 숫자 저장
		//  예) x(3) + y(4) = 7
		int z = 0;
		if(op == 1) {
			z = x + y;
		}else if(op == 2) {
			z = x - y;
		}else if(op == 3) {
			z = x * y;
		}
		
		// 4. 화면 문제 출제
		System.out.println(x + " ? " + y + " = " + z);
		System.out.println("?에 들어갈 연산자는?");
		System.out.println("  [1].+  [2].-  [3].*  ");
		
		// 5. 정답 입력받기
		System.out.println();
		System.out.print("정답 입력 => ");
		
		int myAnswer = sc.nextInt();
		
		// 6. 결과 출력하기
		if(op == myAnswer) {
			System.out.println("정답");
		}else {
			System.out.println("오답");
		}
	}

}

 

 

결과

 

+ Recent posts