C# 숫자맞추기 게임

Park Yeeun·2023년 6월 30일
0

📌 프론트

label textBox Button으로 구현

label 속성
TextAlign : MiddleCenter
Dock : Top
AutoSize : False


💬 코드

public partial class Form1 : Form
{
  private int findNumber = 0;     // 랜덤으로 생성된 번호 저장
  private int chance = 0;         // 도전횟수

  private void ButtonStart_Click(object sender, EventArgs e)
  {
    var rand = new Random();
    findNumber = rand.Next(1, 21);  // 1 ~ 20
    chance = 10;
    display.Text = "맞출 숫자를 입력하세요.";

  }

  private void ButtonInput_Click(object sender, EventArgs e)
  {
    int inputNumber=Int32.Parse(textBox.Text);

    if (inputNumber == findNumber)
    {
    	display.Text = "성공했습니다!";
    }
    else
    {
    	chance--;
    	display.Text = "기회는 " + chance + "번 남았습니다.";

    }
    
    if (chance <= 0)
    {
    	display.Text = "실패했습니다.";
    }
  }

}

💻 실행결과

profile
새싹 개발자입니당 🌱

0개의 댓글