이 4개의 자습서 시리즈에서는 수학 퀴즈를 작성합니다. 퀴즈에는 퀴즈 응시자에서 지정된 시간 내에 대답하려고 시도하는 네 가지 임의 수학 문제가 포함되어 있습니다.
퀴즈는 타이머 컨트롤을 사용합니다. 이 컨트롤 뒤에 있는 코드는 경과된 시간을 추적하고 퀴즈 응시자의 답변을 확인합니다.
이 세 번째 자습서에서는 다음 방법을 알아봅니다.
- 타이머 컨트롤을 추가합니다.
- 타이머에 대한 이벤트 처리기를 추가합니다.
- 코드를 작성하여 사용자의 답변을 확인하고, 메시지를 표시하고, 정답을 채웁니다.
필수 구성 요소
이 자습서는 수학 퀴즈 WinForms 앱만들기부터 시작하여 이전 자습서를 기반으로 합니다. 이러한 자습서를 완료하지 않은 경우, 먼저 자습서를 수행하세요.
카운트다운 타이머 추가
퀴즈 중에 시간을 추적하려면 타이머 구성 요소를 사용합니다. 남은 시간을 저장하려면 변수도 필요합니다.
이전 자습서에서 변수를 선언한 것과 동일한 방식으로 timeLeft 명명된 정수 변수를 추가합니다. timeLeft 선언을 다른 선언 바로 다음에 배치합니다. 코드는 다음 샘플과 같습니다.
public partial class Form1 : Form { // Create a Random object called randomizer // to generate random numbers. Random randomizer = new Random(); // These integer variables store the numbers // for the addition problem. int addend1; int addend2; // These integer variables store the numbers // for the subtraction problem. int minuend; int subtrahend; // These integer variables store the numbers // for the multiplication problem. int multiplicand; int multiplier; // These integer variables store the numbers // for the division problem. int dividend; int divisor; // This integer variable keeps track of the // remaining time. int timeLeft;
Windows Forms 디자이너에서 도구 상자의 구성 요소 범주에 있는 Timer 컨트롤을 양식으로 이동합니다. 디자인 창 아래쪽의 회색 영역에 컨트롤이 나타납니다.
양식에서 방금 추가한 타이머1 아이콘을 선택하고 Interval 속성을 1000설정합니다. 이 간격은 밀리초 단위이므로 값이 1000이면 타이머가 1초마다 Tick 이벤트를 발생합니다.
답변 확인
타이머는 1초마다 Tick 이벤트를 발생하므로 Tick 이벤트 처리기에서 경과된 시간을 확인하는 것이 좋습니다. 또한 해당 이벤트 처리기에서 답변을 확인하는 것도 실용적입니다. 시간이 다 되었거나 답변이 올바른 경우 퀴즈가 끝나야 합니다.
해당 이벤트 처리기를 작성하기 전에 CheckTheAnswer()
메서드를 추가하여 수학 문제에 대한 답변이 올바른지 확인합니다. 이 메서드는 StartTheQuiz()
같은 다른 메서드와 일치해야 합니다. 코드는 다음 샘플과 같습니다.
/// <summary>
/// Check the answers to see if the user got everything right.
/// </summary>
/// <returns>True if the answer's correct, false otherwise.</returns>
private bool CheckTheAnswer()
{
if ((addend1 + addend2 == sum.Value)
&& (minuend - subtrahend == difference.Value)
&& (multiplicand * multiplier == product.Value)
&& (dividend / divisor == quotient.Value))
return true;
else
return false;
}
이 메서드는 수학 문제에 대한 답을 결정하고 결과를 NumericUpDown 컨트롤의 값과 비교합니다. 이 코드에서는 다음을 수행합니다.
Visual Basic 버전은 이 메서드가 값을 반환하기 때문에 일반적인
Sub
키워드 대신Function
키워드를 사용합니다.키보드를 사용하여 곱하기 기호(×) 및 나누기 기호(÷)를 쉽게 입력할 수 없으므로 C# 및 Visual Basic은 곱하기 위한 별표(*)와 나누기용 슬래시 표시(/)를 허용합니다.
C#에서
&&
logical and
연산자입니다. Visual Basic에서 해당 연산자는AndAlso
.logical and
연산자를 사용하여 둘 이상의 조건이 true인지 확인합니다. 이 경우 값이 모두 올바르면 메서드는true
값을 반환합니다. 그렇지 않으면 메서드는false
값을 반환합니다.if
문은 NumericUpDown 컨트롤의 Value 속성을 사용하여 컨트롤의 현재 값에 액세스합니다. 다음 섹션에서는 동일한 속성을 사용하여 각 컨트롤에 정답을 표시합니다.
타이머에 이벤트 처리기 추가
이제 답변을 확인할 수 있으므로 Tick 이벤트 처리기에 대한 코드를 작성할 수 있습니다. 타이머가 틱(Tick) 이벤트를 발생시킨 후 이 코드는 매초마다 실행됩니다. 이 이벤트 처리기는 CheckTheAnswer()
호출하여 퀴즈 응시자의 답변을 확인합니다. 또한 퀴즈에서 경과된 시간을 확인합니다.
양식에서 타이머 컨트롤을 두 번 클릭하거나 선택한 다음 Enter를 누릅니다. 이러한 작업은 Tick 이벤트 처리기를 추가합니다. 코드 편집기가 나타나고 틱 처리기의 메서드가 표시됩니다.
C#의 경우 Form1.Designer.cs 코드 파일에 이벤트 처리기를 연결하는 코드 줄을 추가합니다.
timer1.Tick += new EventHandler(timer1_Tick);
(Visual Basic의 경우 해당 줄은 필요하지 않지만 이벤트 처리기에는 동일한 작업을 수행하는
handles Timer1.Tick
포함되어 있습니다.)새 이벤트 처리기 메서드에 다음 문을 추가합니다.
private void timer1_Tick(object sender, EventArgs e) { if (CheckTheAnswer()) { // If CheckTheAnswer() returns true, then the user // got the answer right. Stop the timer // and show a MessageBox. timer1.Stop(); MessageBox.Show("You got all the answers right!", "Congratulations!"); startButton.Enabled = true; } else if (timeLeft > 0) { // If CheckTheAnswer() returns false, keep counting // down. Decrease the time left by one second and // display the new time left by updating the // Time Left label. timeLeft = timeLeft - 1; timeLabel.Text = timeLeft + " seconds"; } else { // If the user ran out of time, stop the timer, show // a MessageBox, and fill in the answers. timer1.Stop(); timeLabel.Text = "Time's up!"; MessageBox.Show("You didn't finish in time.", "Sorry!"); sum.Value = addend1 + addend2; difference.Value = minuend - subtrahend; product.Value = multiplicand * multiplier; quotient.Value = dividend / divisor; startButton.Enabled = true; } }
퀴즈의 매 초마다 이 메서드가 실행됩니다. 코드는 먼저 CheckTheAnswer()
반환하는 값을 확인합니다.
모든 답변이 올바르면 해당 값이
true
이고, 퀴즈가 끝납니다.- 타이머가 중지됩니다.
- 축하 메시지가 나타납니다.
-
startButton 컨트롤의 Enabled 속성이
true
으로 설정되어 퀴즈 응시자가 다른 퀴즈를 시작할 수 있도록 합니다.
CheckTheAnswer()
false
반환하는 경우 코드는 timeLeft값을 확인합니다.- 이 변수가 0보다 크면 타이머는 timeLeft에서 1을 뺍니다. 또한 timeLabel 컨트롤의 Text 속성을 업데이트하여 퀴즈 소요 시간(초)을 표시합니다.
- 시간이 남아 있지 않으면 타이머가 중지되고 timeLabel 텍스트가 Time's up으로 변경됩니다. 메시지 상자가 퀴즈가 끝났다고 알리고 답변이 표시됩니다. 시작 단추를 다시 사용할 수 있게 됩니다.
타이머 시작
퀴즈가 시작될 때 타이머를 시작하려면 다음 샘플과 같이 StartTheQuiz()
메서드의 끝에 세 줄을 추가합니다.
/// <summary>
/// Start the quiz by filling in all of the problem
/// values and starting the timer.
/// </summary>
public void StartTheQuiz()
{
// Fill in the addition problem.
// Generate two random numbers to add.
// Store the values in the variables 'addend1' and 'addend2'.
addend1 = randomizer.Next(51);
addend2 = randomizer.Next(51);
// Convert the two randomly generated numbers
// into strings so that they can be displayed
// in the label controls.
plusLeftLabel.Text = addend1.ToString();
plusRightLabel.Text = addend2.ToString();
// 'sum' is the name of the NumericUpDown control.
// This step makes sure its value is zero before
// adding any values to it.
sum.Value = 0;
// Fill in the subtraction problem.
minuend = randomizer.Next(1, 101);
subtrahend = randomizer.Next(1, minuend);
minusLeftLabel.Text = minuend.ToString();
minusRightLabel.Text = subtrahend.ToString();
difference.Value = 0;
// Fill in the multiplication problem.
multiplicand = randomizer.Next(2, 11);
multiplier = randomizer.Next(2, 11);
timesLeftLabel.Text = multiplicand.ToString();
timesRightLabel.Text = multiplier.ToString();
product.Value = 0;
// Fill in the division problem.
divisor = randomizer.Next(2, 11);
int temporaryQuotient = randomizer.Next(2, 11);
dividend = divisor * temporaryQuotient;
dividedLeftLabel.Text = dividend.ToString();
dividedRightLabel.Text = divisor.ToString();
quotient.Value = 0;
// Start the timer.
timeLeft = 30;
timeLabel.Text = "30 seconds";
timer1.Start();
}
퀴즈가 시작되면 이 코드는 timeLeft 변수를 30으로 설정하고 timeLabel 컨트롤의 Text 속성을 30초로 설정합니다. 그런 다음 타이머 컨트롤의 Start() 메서드가 카운트다운을 시작합니다.
앱 실행
프로그램을 저장하고 실행합니다.
퀴즈시작하기를 선택하십시오. 타이머가 카운트다운하기 시작합니다. 시간이 다 떨어지면 퀴즈가 종료되고 답변이 나타납니다.
다른 퀴즈를 시작하고 수학 문제에 대한 올바른 답변을 제공합니다. 시간 제한 내에서 올바르게 응답하면 메시지 상자가 열리고 시작 단추를 사용할 수 있게 되고 타이머가 중지됩니다.
19초 남은 완료된 퀴즈를 보여 주는
다음 단계
다음 자습서로 이동하여 수학 퀴즈를 사용자 지정하는 방법을 알아봅니다.
자습서 4부: 수학 퀴즈 WinForms 앱 사용자 지정