Passaggio 7: aggiungere problemi di moltiplicazione e divisione

Prima di iniziare questa procedura, considerare come si aggiungeranno problemi di moltiplicazione e divisione.Considerare il passaggio iniziale, che comporta l'archiviazione dei valori.

Per aggiungere problemi di moltiplicazione e divisione

  1. Aggiungere quattro int (Integer) al form.Il codice dovrebbe essere analogo al seguente.

    Public Class Form1
    
        ' Create a Random object to generate random numbers.
        Dim randomizer As New Random
    
        ' These Integers will store the numbers in 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
    
        ' These Integers will store the numbers for the multiplication problem.
        Dim multiplicand As Integer
        Dim multiplier As Integer
    
        ' These Integers will store the numbers for the division problem.
        Dim dividend As Integer
        Dim divisor 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;
    
        // 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;
    
  2. Come in precedenza, modificare il metodo StartTheQuiz() per inserire problemi di moltiplicazione e divisione casuali.Il codice dovrebbe essere analogo al seguente.

    ''' <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();
    }
    
  3. Modificare il metodo CheckTheAnswer() in modo che controlli anche i problemi di moltiplicazione e divisione.Il codice dovrebbe essere analogo al seguente.

    ''' <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;
    }
    

    Nota

    Poiché non esiste un modo semplice di immettere il segno di moltiplicazione (×) e il segno di divisione (÷) utilizzando la tastiera, in Visual C# e Visual Basic si utilizza un asterisco (*) per la moltiplicazione e una barra (/) per la divisione.

  4. Modificare l'ultima parte del gestore dell'evento Tick del timer in modo che fornisca la risposta corretta quando il tempo scade.Il codice dovrebbe essere analogo al seguente.

    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;
    }
    
  5. Salvare ed eseguire il programma.Per completare il quiz, è ora necessario che venga data risposta a quattro problemi, come mostrato nell'immagine seguente.

    Quiz matematico con quattro problemi

    Math quiz with four problems

Per continuare o rivedere l'esercitazione