I seem to be having some trouble with my multiple choice program. I can't seem to figure out how to do attempts in a while loop. For example, I give you two attempts and lets say you got the first question wrong. Then it will give you another try at the same question. I think I need to use a while loop I just can't seem to figure out how to implement it. Also, is there A way that I can restart the program if the isn't 100?
string First;
int score = 0;
string Second;
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
Console.Write("Where is the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");
First = Console.ReadLine();
switch (First)
{
case "B":
Console.WriteLine("You entered the correct answer!");
break;
case "A":
Console.WriteLine("You entered the wrong answer.");
break;
case "C":
Console.WriteLine("You entered the wrong answer.");
break;
case "D":
Console.WriteLine("You entered the wrong answer.");
break;
default:
Console.WriteLine("You did not enter a correct answer.");
break;
}
if (First == "B")
{
score = score + 50;
Console.WriteLine("Correct!\n" + " score:" + score + "\n");
}
else
{
Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
}
Console.Write("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");
Second = Console.ReadLine();
switch (Second)
{
case "A":
Console.Write("You entered the correct answer!");
break;
case "B":
Console.WriteLine("You entered the wrong answer.");
break;
case "C":
Console.WriteLine("You entered the wrong answer.");
break;
case "D":
Console.WriteLine("You entered the wrong answer.");
break;
default:
Console.WriteLine("You did not enter a correct answer.");
break;
}
if (Second == "A")
{
score = score + 50;
Console.WriteLine("Correct!\n" + " score:" + score + "\n");
}
else
{
Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
}
As stated by others, you should change the approach of your code to make it more flexible and avoid needless duplication. However, if I were to follow your structure, you could use an integer variable to store the amount of attempts left, and decrease it each time an incorrect answer is submited. Encase the code of each question in a while loop where the condition is that the number of attempts left is > 0. Example:
string First;
int score = 0;
int attempts = 2; //our integer variable
string Second;
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
while (attempts > 0)
{
//First question
Console.WriteLine("Where is the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); //use WriteLine instead of Write, for console clarity
First = Console.ReadLine();
switch (First.ToUpper()) //accept lowercase inputs
{
case "B": Console.WriteLine("You entered the correct answer!"); break;
case "A": case "C": case "D": Console.WriteLine("You entered the wrong answer."); break; //same logic
default: Console.WriteLine("You did not enter a correct answer."); break;
}
if (First.ToUpper() == "B")
{
score = score + 50;
Console.WriteLine("Correct!\n" + " score:" + score + "\n");
break; //force exit loop, onto the next question
}
else
{
Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
attempts--; //decrease number of attempts left
}
}
while(attempts > 0)
{
//Second question
Console.WriteLine("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");
Second = Console.ReadLine();
switch (Second.ToUpper()) //accept lowercase inputs
{
case "A": Console.Write("You entered the correct answer!"); break;
case "B": case "C": case "D": Console.WriteLine("You entered the wrong answer."); break;
default: Console.WriteLine("You did not enter a correct answer."); break;
}
if (Second.ToUpper() == "A")
{
score = score + 50;
Console.WriteLine("Correct!\n" + " score:" + score + "\n");
break; //you win!
}
else
{
Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
attempts--; //decrease number of attempts left
}
}