Reputation: 13
I have a simple program that checks if two numbers added together is == to the textbox answer(mathAnswer), but when I enter in the correct answer and click the submit button I keep getting "Incorrect" from answerstatus label? Does anyone know why?
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)
{
}
}
}
Upvotes: 0
Views: 70
Reputation: 21757
Your comparison has to happen after your user clicks the submit button. Therefore, the comparison code should be placed in the button click event handler for that button, like so:
private void submitAnswer_Click(object sender, RoutedEventArgs e)
{
if (answer.ToString() == mathAnswer.Text)
{
answerStatusLabel.Text = "Correct!";
}
else
{
answerStatusLabel.Text = "Incorrect!";
}
}
Upvotes: 1