手順 7: 乗算問題と除算問題の追加
この手順を開始する前に、乗算問題と除算問題を追加する方法について考えてみてください。最初の手順は、値を格納することです。
乗算問題と除算問題を追加するには
フォームに 4 つの int (Integer) を追加します。コードは次のようになります。
Public Class Form1 ' Create a Random object to generate random numbers. Private randomizer As New Random ' These Integers will store the numbers in the addition problem. Private addend1 As Integer Private addend2 As Integer ' These Integers will store the numbers for the subtraction problem. Private minuend As Integer Private subtrahend As Integer ' These Integers will store the numbers for the multiplication problem. Private multiplicand As Integer Private multiplier As Integer ' These Integers will store the numbers for the division problem. Private dividend As Integer Private divisor As Integer ' This Integer will keep track of the time left. Private 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; // These ints will store the numbers for the multiplication problem. int multiplicand; int multiplier; // These ints will store the numbers for the division problem. int dividend; int divisor; // This int will keep track of the time left. int timeLeft;
前の手順のときと同様に、StartTheQuiz() メソッドを、ランダムな乗算問題と除算問題を表示するように変更します。コードは次のようになります。
''' <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 ' 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) Dim temporaryQuotient As Integer = 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() 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; // 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(); }
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 AndAlso multiplicand * multiplier = product.Value AndAlso dividend / divisor = quotient.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) && (multiplicand * multiplier == product.Value) && (dividend / divisor == quotient.Value)) return true; else return false; }
[!メモ]
乗算記号 (×) と除算記号 (÷) はキーボードから簡単に入力できないため、Visual C# および Visual Basic では、乗算にはアスタリスク (*)、除算にはスラッシュ記号 (/) を使用します。
タイマーの 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 product.Value = multiplicand * multiplier quotient.Value = dividend / divisor 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; product.Value = multiplicand * multiplier; quotient.Value = dividend / divisor; startButton.Enabled = true; }
プログラムを保存し、実行します。これで、次の図に示すように、クイズを完了するために答える必要がある 4 つの問題が揃いました。
4 つの問題が表示された計算クイズ
続行または確認するには
チュートリアルの次の手順に進むには、「手順 8: その他の機能を試す」を参照してください。
チュートリアルの前の手順に戻るには、「手順 6: 減算問題の追加」を参照してください。