Share via


手順 6: 減算問題の追加

減算問題を追加するには、以下を実行する必要があります。

  • 減算の値を格納します。

  • 問題の乱数を生成します (答えが 0 ~ 100 になるようにします)。

  • 答えを確認するメソッドを、新しい減算問題についても確認するように更新します。

  • タイマーの Tick イベント ハンドラーを、残り時間がなくなったら正しい答えを表示するように更新します。

減算問題を追加するには

  1. まず、値を格納する場所が必要なため、減算問題用の 2 つの int (Integer) をフォームに追加します。新しいコードは、加算の整数とタイマーの整数の間に記述します。コードは次のようになります。

    Public Class Form1
    
        ' Create a Random object to generate random numbers.
        Dim randomizer As New Random
    
        ' These Integers will store the numbers
        ' for the addition problem.
        Dim addend1 As Integer
        Dim addend2 As Integer
    
        ' These Integers will store the numbers
        ' for the subtraction problem.
        Dim minuend As Integer
        Dim subtrahend As Integer
    
        ' This Integer will keep track of the time left.
        Dim timeLeft As Integer
    
    public partial class Form1 : Form
    {
        // Create a Random object to generate random numbers.
        Random randomizer = new Random();
    
        // These ints will store the numbers
        // for the addition problem.
        int addend1;
        int addend2;
    
        // These ints will store the numbers
        // for the subtraction problem.
        int minuend;
        int subtrahend;
    
        // This int will keep track of the time left.
        int timeLeft;
    

    注意

    新しい int の名前 (minuend および subtrahend) は、Visual C# の用語でもプログラミング用語でもありません。これらは、減算する数値 (subtrahend/減数) と減算される数値 (minuend/被減数) を表す従来の数学用語です。被減数から減数を引いたものが差になります。int、コントロール、コンポーネント、またはメソッドの名前を特定の名前にするようにプログラムで制限されているわけではないため、別の名前を使用することもできます。いくつか規則はありますが (名前の先頭を数字にすることはできないなど)、通常は x1、x2、x3、x4 などの名前を使用できます。しかし、このような名前だとコードが読みにくくなり、問題の追跡がほとんどできなくなります。このチュートリアルでは、乗算 (被乗数 × 乗数 = 積) および除算 (被除数 ÷ 除数 = 商) についても従来の名前を使用します。

  2. 次に、StartTheQuiz() メソッドを、ランダムな減算問題を表示するように変更します。新しいコードは、"Fill in the subtraction problem" というコメントの後に記述します。コードは次のようになります。

    ''' <summary>
    ''' Start the quiz by filling in all of the problems
    ''' and starting the timer.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub StartTheQuiz()
    
        ' Fill in the addition problem.
        addend1 = randomizer.Next(51)
        addend2 = randomizer.Next(51)
        plusLeftLabel.Text = addend1.ToString
        plusRightLabel.Text = addend2.ToString
        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
    
        ' Start the timer.
        timeLeft = 30
        timeLabel.Text = "30 seconds"
        Timer1.Start()
    
    End Sub
    
    /// <summary>
    /// Start the quiz by filling in all of the problems
    /// and starting the timer.
    /// </summary>
    public void StartTheQuiz()
    {
        // Fill in the addition problem.
        addend1 = randomizer.Next(51);
        addend2 = randomizer.Next(51);
        plusLeftLabel.Text = addend1.ToString();
        plusRightLabel.Text = addend2.ToString();
        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;
    
        // Start the timer.
        timeLeft = 30;
        timeLabel.Text = "30 seconds"; 
        timer1.Start();
    }
    

    このコードでは、Random クラスの Next() メソッドを少し異なる方法で使用しています。このメソッドに値を 2 つ指定した場合、1 つ目の値以上で 2 つ目の値未満の乱数が選択されます。次のコード行では、1 ~ 100 の乱数が選択され、minuend に格納されます。

    minuend = randomizer.Next(1, 101)
    
    minuend = randomizer.Next(1, 101);
    

    Random クラスの Next() メソッドは、いくつかの方法で呼び出すことができます。このような複数の方法で呼び出すことができるメソッドのことをオーバーロード メソッドと呼び、IntelliSense を使用して確認することができます。IntelliSense ウィンドウの Next() メソッドに対するツールヒントをもう一度見てください。

    IntelliSense ウィンドウのツールヒント

    次のメソッド

    ツールヒントに "(+ 2 オーバーロード)" と表示されています。これは、Next() メソッドを呼び出す方法があと 2 つあることを示しています。StartTheQuiz() メソッドの新しいコードを入力すると、さらに情報が表示されます。「randomizer.Next(,」まで入力したところで、IntelliSense ウィンドウが開きます。上方向キーおよび下方向キーを押すと、次の図に示すように、別のオーバーロードに切り替わります。

    IntelliSense ウィンドウのオーバーロード

    IntelliSense ウィンドウのオーバーロード

    上の図に表示されたオーバーロードが最小値と最大値を指定できるものであるため、ここではこのオーバーロードを使用します。

  3. CheckTheAnswer() メソッドを、減算の答えが正しいかどうかを確認するように変更します。コードは次のようになります。

    ''' <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) AndAlso (minuend - subtrahend = difference.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)
            && (minuend - subtrahend == difference.Value))
            return true;
        else
            return false;
    }
    

    && は、Visual C# の logical and 演算子です。Visual Basic でこれに相当する演算子は AndAlso です。これは、"addend1 に addend2 を加えた値が sum NumericUpDown と等しい場合、かつ minuend から subtrahend を引いた値が difference NumericUpDown と等しい場合" という意味になります。つまり、CheckTheAnswer() メソッドは、加算問題と減算問題の両方に正解した場合にのみ true を返します。

  4. タイマーの Tick イベント ハンドラーの最後の部分を、残り時間がなくなったら正しい答えを表示するように変更します。コードは次のようになります。

    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
        startButton.Enabled = True
    End If
    
    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;
        startButton.Enabled = true;
    }
    
  5. コードを保存し、実行します。次の図に示すように、プログラムで減算問題が表示されるようになります。

    減算問題が表示された計算クイズ

    減算の問題のある計算クイズ

続行または確認するには