Share via


手順 4: CheckTheAnswer() メソッドの追加

クイズではユーザーの答えが正しいかどうかを確認する必要があります。そのため、ここでは CheckTheAnswer() というメソッドを記述しますが、このような単純な計算を実行するメソッドは簡単に記述することができます。

注意

Visual Basic を使用している場合、このメソッドは値を返すため、通常の Sub キーワードではなく、Function キーワードを使用することに注意してください。これは単に、Sub は値を返さないため、値を返す Function を使用しているだけです。

CheckTheAnswer() メソッドを追加するには

  1. CheckTheAnswer() メソッドを追加します。このメソッドでは、addend1 と addend2 を加算し、その和が sum NumericUpDown コントロールの値と等しいかどうかを確認します。和が等しい場合は true を返し、等しくない場合は false を返します。コードは次のようになります。

    ''' <summary>
    ''' Check the answer to see if the user got everything right.
    ''' </summary>
    ''' <returns>True if the answer's correct, false otherwise.</returns>
    ''' <remarks></remarks>
    Public Function CheckTheAnswer() As Boolean
    
        If addend1 + addend2 = sum.Value Then
            Return True
        Else
            Return False
        End If
    
    End Function
    
    /// <summary>
    /// Check the answer 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)
            return true;
        else
            return false;
    }
    

    プログラムでこのメソッドを呼び出して、ユーザーの答えが正しいかどうかを確認する必要があります。これを行うには、if else ステートメントにコードを追加します。ステートメントは次のようになります。

    If CheckTheAnswer() Then
        ' statements that will get executed
        ' if the answer is correct 
    ElseIf timeLeft > 0 Then
        ' statements that will get executed
        ' if there's still time left on the timer
    Else
        ' statements that will get executed if the timer ran out
    End If
    
    if (CheckTheAnswer())
    {
          // statements that will get executed
          // if the answer is correct 
    }
    else if (timeLeft > 0)
    {
          // statements that will get executed
          // if there's still time left on the timer
    }
    else
    {
          // statements that will get executed if the timer ran out
    }  
    
  2. 次に、タイマーの Tick イベント ハンドラーを、答えを確認するように変更します。答えを確認する新しいイベント ハンドラーは次のようになります。

    Private Sub Timer1_Tick() Handles Timer1.Tick
    
        If CheckTheAnswer() Then
            ' If the user got the answer right, stop the timer
            ' and show a MessageBox.
            Timer1.Stop()
            MessageBox.Show("You got all of the answers right!", "Congratulations!")
            startButton.Enabled = True
        ElseIf timeLeft > 0 Then
            ' 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
            startButton.Enabled = True
        End If
    
    End Sub
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (CheckTheAnswer())
        {
            // If 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)
        {
            // Decrease the time left by one second and display 
            // the new time left by updating the Time Left label.
            timeLeft--;
            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;
            startButton.Enabled = true;
        }
    }
    

    タイマーのイベント ハンドラーは、ユーザーの答えが正しいことを確認すると、タイマーを停止し、"Congratulations" というメッセージを表示し、[Start] ボタンを再び使用できるようにします。

  3. プログラムを保存し、実行します。ゲームを開始し、加算問題の正しい答えを入力します。

    注意

    この時点では、答えを入力するときの NumericUpDown コントロールの動作が適切ではありません。答え全体を選択せずに入力を開始した場合、0 がそのまま残り、手動で削除する必要があります。これについては、このチュートリアルの以降の手順で修正します。

  4. 正しい答えを入力すると、メッセージ ボックスが開き、[Start] ボタンが使用できるようになり、タイマーが停止します。[Start] ボタンをもう一度クリックし、同じ処理が行われることを確認します。

続行または確認するには