Eggs Sunny Side Up
본문 바로가기
언어/JAVA

while문_랜덤으로 정수 2개를 뽑아 합 입력

by guswn100059 2023. 3. 1.

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

public class Ex_연습장 {

	public static void main(String[] args) {
		
		
		Random rd = new Random();
		Scanner sc = new Scanner(System.in);
		
		int cnt = 0 ;
		
		while(true) {
			int num1 = rd.nextInt(10)+1;
			int num2 = rd.nextInt(10)+1;
			
			System.out.print(num1+"+"+num2+"=");
			
			int result = sc.nextInt();
			
			if(num1+num2 == result) {
				System.out.println("SUCCESS!");
			} else {
				System.out.println("FAIL...");
				
				cnt++;
				
				if(cnt == 5) {
					System.out.println("GAME OVER!");
					break;
				}
				
			}
			
			
		}
		

		
		
			}

	}

댓글