숫자 찾기 게임

지금까지 공부한 것을 응용하여 숫자 맞추기 게임 프로그램을 만들어보자.

프로그램이 최소값과 최대값 사이의 숫자를 랜덤으로 설정하면 사용자가 숫자를 추측하는 게임이다.

    Random random = new Random();
    bool playAgain = true;
    int min = 1;
    int max = 100;
    int guess;
    int number;
    int guesses;
    String response;

    while (playAgain)
    {
        guess = 0;
        guesses = 0;
        response = "";
        number = random.Next(min, max + 1);

        while (guess != number)
        {
            Console.WriteLine(min + " - " + max + "사이의 숫자를 입력하세요.");
            guess = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("정답: " + guess);

            if (guess > number)
            {
                Console.WriteLine(guess + "보다 작습니다.");
            }
            else if (guess < number)
            {
                Console.WriteLine(guess + "보다 큽니다.");
            }
            guesses++;
        }
        Console.WriteLine("숫자: " + number);
        Console.WriteLine("정답입니다!");
        Console.WriteLine("시도 횟수: " + guesses);

        Console.WriteLine("다시하기 (Y/N): ");
        response = Console.ReadLine();
        response = response.ToUpper();

        if (response == "Y")
        {
            playAgain = true;
        }
        else
        {
            playAgain = false;
        }
    }
    Console.WriteLine("플레이 해주셔서 감사합니다.");
}

playAgain에 true를 할당하여  다시 시작했을 때 초기값을 설정해준다. 사용자가 숫자를 입력 했을 때 숫자가 너무 높거나 낮으면 while문을 통해 힌트를 준다. 정답을 맞추면 시도 횟수와 함께 다시 할 것인지 묻는다. if문을 통해 사용자의 대답을 처리한다.

 

 

'C# > C#' 카테고리의 다른 글

19. 계산기 프로그램(Calculator Program)  (0) 2023.09.13
18. 가위바위보 게임(Rock-Paper-Scissors Game)  (0) 2023.09.07
16. 중첩 반복문(Nested Loops)  (0) 2023.09.04
15. For문(For Loops)  (0) 2023.08.31
14. While문(While Loops)  (0) 2023.08.31
중첩 반복문

반복문 안의 반복문으로 정렬 알고리즘에 많이 사용된다.

 

중첩 반복문을 이용하여 원하는 기호로 직사각형을 만드는 프로그램을 만들어보자.

Console.Write("행의 갯수를 입력하세요. : ");
int rows = Convert.ToInt32(Console.ReadLine());

Console.Write("열의 갯수를 입력하세요. : ");
int columns = Convert.ToInt32(Console.ReadLine());

Console.Write("기호를 입력하세요. : ");
String symbol = Console.ReadLine();

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        Console.Write(symbol);
    }
    Console.WriteLine();
}

외부 반복문이 실행되면 즉시 내부 반복문이 실행된다. 내부 반복문이 모두 끝나야 외부 반복문 한 번이 반복된 것이다. 내부 반복문이 끝날 때마다 한 줄씩 넘어가기 위해 Console.WriteLine();을 넣는다. 이것이 반복되면 우리가 설정한 기호의 직사각형이 완성된다.

 

 

'C# > C#' 카테고리의 다른 글

18. 가위바위보 게임(Rock-Paper-Scissors Game)  (0) 2023.09.07
17. 숫자 맞추기 게임(Number Guessing Game)  (0) 2023.09.05
15. For문(For Loops)  (0) 2023.08.31
14. While문(While Loops)  (0) 2023.08.31
13. 논리 연산자(Logical Operators)  (0) 2023.08.31
for문

조건이 참인 동안 일부 코드를 유한하게 반복한다.

for (int i = 1; i <= 10; i++)
{
    Console.WriteLine(i);
}

 

 

원하는 만큼 증가시킬 수 있다.

2씩 증가

for (int i = 1; i <= 10; i+=2)
{
    Console.WriteLine(i);
}

3씩 증가

for (int i = 1; i <= 10; i+=3)
{
    Console.WriteLine(i);
}

 

감소시킬 수도 있다.

for (int i = 10; i > 0; i--)
{
    Console.WriteLine(i);
}
Console.WriteLine("새해 복 많이 받으세요!");

 

while문의 어떤 코드는 무한하게 반복된다. 그와 달리 for문은 제한적으로 반복된다.

 

 

'C# > C#' 카테고리의 다른 글

17. 숫자 맞추기 게임(Number Guessing Game)  (0) 2023.09.05
16. 중첩 반복문(Nested Loops)  (0) 2023.09.04
14. While문(While Loops)  (0) 2023.08.31
13. 논리 연산자(Logical Operators)  (0) 2023.08.31
12. 조건문(Switches)  (0) 2023.08.28
while문

조건이 참인 동안 일부 코드를 반복한다.

Console.Write("이름을 입력하세요. : ");
String name = Console.ReadLine();

while (name == "")
{
    Console.Write("이름을 입력하세요. : ");
    name = Console.ReadLine();
}

Console.WriteLine("안녕하세요 " + name);

 

조건이 늘 참이게 되면 무한루프에 빠질 수 있으니 주의해야한다.

while (1 == 1)
{
    Console.WriteLine("도와주세요! 무한루프에 갇혀있어요!");
}

 

 

'C# > C#' 카테고리의 다른 글

16. 중첩 반복문(Nested Loops)  (0) 2023.09.04
15. For문(For Loops)  (0) 2023.08.31
13. 논리 연산자(Logical Operators)  (0) 2023.08.31
12. 조건문(Switches)  (0) 2023.08.28
11. 조건문(If Statement)  (0) 2023.08.25

+ Recent posts