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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2011, 4:18 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2011, 4:27 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 31, 2011, 9:03 AM EDT
Antony Fox10 reputationPosted Mar 31, 2011, 9:05 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 31, 2011, 11:34 AM EDT
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 Fox10 reputationPosted Apr 18, 2011, 11:27 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 18, 2011, 12:29 PM EDT
for (int i = 0; i < 15; i++)
{
questions[i] = i.ToString();
answers[i] = (i * i).ToString();
}