数学逻辑不起作用

本文关键字:不起作用 | 更新日期: 2023-09-27 18:15:50

我有一个简单的程序,检查两个数字加在一起是否==文本框答案(mathAnswer),但是当我输入正确的答案并单击提交按钮时,我一直从answerstatus标签获得"不正确" ?有人知道为什么吗?

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace FinalProjectPhoneVersion
{
    public partial class MathPage : PhoneApplicationPage
    {
        public MathPage()
        {
                    Random random = new Random();

                    int randomNumber1 = random.Next(0, 10);
                    int randomNumber2 = random.Next(0, 5);
         int answer = randomNumber1 + randomNumber2;
            int counter = 0;
            int strikeCounter=0;
            InitializeComponent();
            String welcomeString = (String)PhoneApplicationService.Current.State["enterNameBox"];
            RadioButton hardRadio = (RadioButton)PhoneApplicationService.Current.State["radio_button2"];
            RadioButton easyRadio = (RadioButton)PhoneApplicationService.Current.State["radio_button1"];
            welcomeLabel.Text = "Welcome " + welcomeString;
        //    difficultyLevelLabel.Text = "Difficulty Level: " + easyMode;
            scoreLabel.Text = "Score: " + counter;
            strikerCounterLabel.Text = "Answers Wrong: " + strikeCounter;
            if ((bool)easyRadio.IsChecked == true)
            {
                randomNumber1 = random.Next(0, 10);
                randomNumber2 = random.Next(0, 5);

            }
            else if ((bool)hardRadio.IsChecked == true) {
                randomNumber1 = random.Next(0, 6);
                randomNumber2 = random.Next(0, 100);
            }
            if (answer.ToString() == mathAnswer.Text) {
                answerStatusLabel.Text = "Correct!";
            }
            else if (answer.ToString() != mathAnswer.Text)
            {
                answerStatusLabel.Text = "Incorrect!";
            }
            scoreLabel.Text = "Score: " + counter.ToString();
          //  if (difficultyList.SelectedIndex == 0)
          //  {

             //   mathQuestion.Text = Convert.ToString(randomNumber2) + " + " + Convert.ToString(randomNumber2);//add this line 
                num1Label.Text = Convert.ToString(randomNumber1);//add this line 
                num2Label.Text = Convert.ToString(randomNumber2); //and this line
                mathSign.Text = "+";
          //  }
          //  else if (difficultyList.SelectedIndex == 1)
          //  {
               // answer = randomNumber1 * randomNumber2;
             //   num1Label.Text = Convert.ToString(randomNumber2);//add this line 
               // num2Label.Text = Convert.ToString(randomNumber1); //and this line
         //   }
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
        }
        private void submitAnswer_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}

数学逻辑不起作用

必须在用户单击提交按钮之后进行比较。因此,比较代码应该放在该按钮的按钮单击事件处理程序中,如下所示:

private void submitAnswer_Click(object sender, RoutedEventArgs e)
    {
      if (answer.ToString() == mathAnswer.Text) 
        {
            answerStatusLabel.Text = "Correct!";
        }
      else 
        {
            answerStatusLabel.Text = "Incorrect!";
        }
    }