본문 바로가기
언어/[C#]

[C#] 숫자 맞추기 게임 (number guess game) - 업다운 (up and down)

by 조랩 2023. 3. 1.
using System;

namespace MyFirstProgram
{
    internal class Program
    {

        static void Main(string[] args) // main method
        {

            // 숫자 맞추기 게임 (업다운)

            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;
                number = random.Next(min, max + 1);
                response = "";

                while (guess != number)
                {
                    Console.WriteLine("Guess a number between " + min + " - " + max + " : ");
                    guess = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine("Guess: " + guess);

                    if (guess > number)
                    {
                        Console.WriteLine(guess + " is to high!");
                    }else if (guess < number)
                    {
                        Console.WriteLine(guess + " is to low!");
                    }
                    guesses++;
                }

                Console.WriteLine("Number: " + number);
                Console.WriteLine("You Win!!");
                Console.WriteLine("Guesses: " + guesses);

                Console.WriteLine("Would you like to play again? (Y/N): ");
                response = Console.ReadLine();
                
                if (response == "N")
                {
                    playAgain = false;
                }
            }

            Console.WriteLine("Thanks for playing!");

        }
    }
}
728x90

'언어 > [C#]' 카테고리의 다른 글

[C#] 계산기: Calculator  (0) 2023.03.12
[C#] 가위 바위 보: Rock Scissors Paper  (0) 2023.03.12
[C#] 다중 for문 (nested for loop)  (0) 2023.03.01
[C#] for문 (for loop)  (0) 2023.03.01
[C#] while문 (while loop)  (0) 2023.03.01