How To Loop Questions In C#
I Need This Code To Loop 15 Times So It Asks The User 15 Different Questions With New Random Numbers ...
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Apr 18, 2011, 4:29 PM
for (int i = 0; i < 15; i++)
{
questions[i] = i.ToString();
answers[i] = (i * i).ToString();
}
Antony FoxPosted Apr 18, 2011, 3:27 PM
VulpesPosted Mar 31, 2011, 3:34 PM
1. two buttons - btnStart to load the first question and btnSubmit to submit an answer.
2, two textboxes - txtQuestion to display the question and txtAnswer for the user to enter the answer, before pressing btnSubmit.
Then the code you'll need for your main form, Form1 say, is as follows:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// private fields
private string[] questions;
private string[] answers;
private bool[] alreadyAsked;
private Random r = new Random();
private int numCorrect = 0;
private int numAsked = 0;
private int numQuestion = 0;
private void Form1_Load(object sender, EventArgs e)
{
questions = new string[15];
answers = new string[15];
alreadyAsked = new bool[15];
// populate questions & answers (numbers used for testing)
for (int i = 0; i < 15; i++)
{
questions[i] = i.ToString();
answers[i] = i.ToString();
}
}
private void GetNextQuestion()
{
while (true)
{
int num = r.Next(0, 15);
if (!alreadyAsked[num])
{
alreadyAsked[num] = true;
numQuestion = num;
txtQuestion.Text = questions[num];
txtAnswer.Focus();
return;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
GetNextQuestion();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string answer = txtAnswer.Text.Trim();
if (answer == "")
{
MessageBox.Show("You haven't answered it yet!");
txtAnswer.Focus();
return;
}
if (answer == answers[numQuestion])
{
MessageBox.Show ("That's correct!");
numCorrect++;
}
else
{
MessageBox.Show("Sorry, that's wrong :(");
}
numAsked++;
txtQuestion.Text = "";
txtAnswer.Text = "";
if (numAsked == 15)
{
MessageBox.Show(String.Format("You got {0} question(s) correct", numCorrect));
this.Close();
}
else
{
GetNextQuestion();
}
}
}
}
Antony FoxPosted Mar 31, 2011, 1:05 PM
Suthish NairPosted Mar 31, 2011, 1:03 PM
VulpesPosted Mar 29, 2011, 8:27 AM
VulpesPosted Mar 29, 2011, 8:18 AM
string[] questions = new string[15];
string[] answers = new string[15];
// populate questions & answers
Random r = new Random();
int numCorrect = 0;
for(int i = 0; i < 15; i++)
{
int num = r.Next(0, 15);
Console.WriteLine(questions[num]);
string answer = Console.ReadLine();
if (answer == answers[num])
{
Console.WriteLine("That's correct!");
numCorrect++;
}
else
{
Console.WriteLine("Sorry, that's wrong :(");
}
Console.WriteLine();
}
Console.WriteLine("You got {0} question(s) correct", numCorrect);
You didn't say whether you wanted to avoid asking the same question more than once but, if you do, then that's easy to adjust for.