如何根据主要输入创建辅助循环。 [英] How do I create a secondary loop based on inputs from the primary.
本文介绍了如何根据主要输入创建辅助循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为课堂创建一个测验。这是十个问题和多种选择。我有第一个循环,它运行得足够顺利。然而,这个测验的组成部分需要第二个循环来运行错误回答的问题,即第二次机会。如何创建二次循环。
I'm creating a quiz for class. It's ten questions and multiple choice. I have the first loop down and it runs smoothly enough. However, i component of this quiz requires a second loop to run for questions which are answered wrong, aka second chance. How do create that secondary loop.
<pre>using System;
using System.Linq;
namespace Checkpoint_3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Quiz");
//Variables
bool answerCorrect = false; // will be used to evaluate if answer is correct or not
string[] questions = { "2+2=", "4x5=", "8+2=", "2x100=", "3+3=", "2+100", "2-2=", "1+2=", "3x3=15", "45-5+8=48" }; // will be questions used on test. 8 Multiple choice and 2 true/false
string[] answers = { "a.4 b.2 c.6 d. 10", "a.40 b.20 c.16 d. 11", "a.6 b.12 c.16 d. 10", "a.104 b.200 c.65 d. 102", "a.4 b.2 c.6 d. 10", "a.14 b.200 c.60 d. 102", "a.4 b.2 c.0 d. 10", "a.3 b.2 c.6 d. 102", "true false", " true false" }; // these will stand as answer bank for all questions
string [] correctanswer = { "a", "b", "d", "b", "c", "d", "c", "a", "false", "true" }; //these are the correct answers to all the question. any other selections will be countd as incorrect
int[] scoreCard = { }; // everyone starts with 0 correct out of ten. once quiz is completed score will be listed as 1-10.
//Welcome To Program
Console.WriteLine("Ryan Hinds, ENGR101- Quiz");
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(questions[i]);
Console.WriteLine();
Console.WriteLine(answers[i]);
Console.WriteLine("Answer");
string input = Console.ReadLine(); char letter; char.TryParse(input, out letter);
if (input==correctanswer[i])
{
Console.WriteLine(" You Are Correct");
Addscore(scoreCard[i]);
}
else
Console.WriteLine("Wrong");
Console.WriteLine();
Console.WriteLine();
}
Console.ReadKey();
}
}
}
我尝试了什么:
我曾尝试使用if和else语句,我不能坚持下去。
What I have tried:
Ive tried working with if and else statements, I cant get one to stick.
推荐答案
您可以使用外部循环获得第二次机会:
You can do it using an outer loop for the second chance:
// Array for correct answers
bool answered[] = new bool[10];
for (int chance = 0; chance < 2; chance++)
{
for (int i = 0; i < 10; i++)
{
// Ask if no correct answer provided so far
if (!answered[i])
{
// Ask the question here
// If correct, set answered[i] to true
}
}
}
int score = 0;
for (int i = 0; i < 10; i++)
{
if (answered[i])
score++;
}
这篇关于如何根据主要输入创建辅助循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文