我有一个简单的程序,检查两个数字是否加在一起= =到文本框答案(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)
{
}
}
}
答案 0 :(得分:1)
您的比较必须在用户点击提交按钮后进行。因此,比较代码应放在该按钮的按钮单击事件处理程序中,如下所示:
private void submitAnswer_Click(object sender, RoutedEventArgs e)
{
if (answer.ToString() == mathAnswer.Text)
{
answerStatusLabel.Text = "Correct!";
}
else
{
answerStatusLabel.Text = "Incorrect!";
}
}