우선 폼 배치는 이렇게 해두었다.
작동 원리는
랜덤 숫자 버튼을 눌러 랜덤한 숫자를 생성한다.
이후 텍스트 박스에 숫자를 쓴뒤 정답 버튼눌러 정답을확인 하는 방식으로만들었고
텍스트 박스를 숫자만 입력되게 할수 있지만
예외처리를 한번 사용해주기 위해 그냥 두고 문자열이 입력 되었을 경우에
예외처리를 해주었다
namespace FiindNumber
{
public partial class Form1 : Form
{
Random randomObj = new Random();
int randomValue;
int inputNumber;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
randomValue = randomObj.Next(1, 10);
answer.Text = "랜덤숫자가 생성 되었습니다";
}
private void button2_Click(object sender, EventArgs e)
{
try
{
inputNumber = Int32.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("숫자를 입력해주세요", "숫자 맞추기");
}
if (inputNumber == randomValue)
{
answer.Text = "정답입니다!";
}
else
{
answer.Text = "틀렸습니다";
}
}
}
}
이런식으로 코드를 구성하여 두었고 실행
정답
문자열을 입력하였을때
https://github.com/jaeil777/Study_Csharp/tree/main/FiindNumber
GitHub - jaeil777/Study_Csharp: Study for c# in visualstudio
Study for c# in visualstudio. Contribute to jaeil777/Study_Csharp development by creating an account on GitHub.
github.com
깃허브에 올려두었다
'c# > c# 폼' 카테고리의 다른 글
c#WINFORM 로그인창 만들기 (0) | 2023.04.14 |
---|---|
WINFORM 기초 (0) | 2023.04.14 |