我的程序是一个测验。当用户得到问题时,问题从一级问题开始变得容易,他们会回到第二级问题,如果问题得到正确,他们就会进入第3级问题,依此类推。这是正常工作(除了测验开始时,程序在进入第二级之前由于某种原因问用户两个一级问题)但是,主要问题是当用户得到错误的问题时程序不会按下上一级问题并问他们另一个问题,光标在右上角反复闪烁。当他们达到顶级问题并得到一个正确的时候也会发生这种情况(该程序必须期待6级问题,但没有6级问题)。问题来自一个文本文件,每个级别有10个问题,总共有50个问题。问题存储为struct包含的文本文件中的字符串结构: 问题编号 问题级别 题 问题答案
这是代码中的问题所在。请帮忙,谢谢。
static void quiz(QuestionStruct[] _quiz)
{
bool asked = true;
int score = 0;
int AmountAsked = 0;
string level = "1";
string ans;
int pos = 1;
var pathToFile = @"..\..\..\Files\questions.txt";
using (StreamReader sr = new StreamReader(pathToFile, true))
{
while (AmountAsked < 20 || score >= 50)
{
Console.Clear();
string whatquestionnum = questions[pos].QuestionNum = sr.ReadLine();
string whatlevel = questions[pos].Level = sr.ReadLine();
//Level 1 questions
while (level == "1" && questions[pos].Level == level)
{
AmountAsked++;
string whatques = questions[pos].Question = sr.ReadLine();
Console.Write(whatques);
string selection = Console.ReadLine();
string decider = questions[pos].answer = sr.ReadLine();
if (selection == decider)
{
level = "2";
score = score + 1;
}
else
{
pos++;
}
}
//Level 2 questions
while (level == "2" && questions[pos].Level == level)
{
AmountAsked++;
questions[pos].Question = sr.ReadLine();
Console.Write(questions[pos].Question);
string selection2 = questions[pos].answer = sr.ReadLine();
ans = Console.ReadLine();
if (ans == selection2)
{
level = "3";
score = score + 2;
}
else
{
level = "1";
//questions[pos].Level == "1";
}
}
//Level 3 questions
while (level == "3" && questions[pos].Level == level)
{
AmountAsked++;
questions[pos].Question = sr.ReadLine();
Console.Write(questions[pos].Question);
string selection3 = questions[pos].answer = sr.ReadLine();
ans = Console.ReadLine();
if (ans == selection3)
{
level = "4";
score = score + 3;
}
else
{
level = "2";
}
}
//Level 4
while (level == "4" && questions[pos].Level == level)
{
AmountAsked++;
questions[pos].Question = sr.ReadLine();
Console.Write(questions[pos].Question);
string selection4 = questions[pos].answer = sr.ReadLine();
ans = Console.ReadLine();
if (ans == selection4)
{
level = "5";
score = score + 4;
}
else
{
level = "3";
}
}
//Level 5
while (level == "5" && questions[pos].Level == level)
{
AmountAsked++;
questions[pos].Question = sr.ReadLine();
Console.Write(questions[pos].Question);
string selection5 = questions[pos].answer = sr.ReadLine();
ans = Console.ReadLine();
if (ans == selection5)
{
level = "5";
score = score + 5;
}
else
{
level = "4";
}
}
}
}
}
答案 0 :(得分:2)
这不起作用,因为您只循环浏览一次文件(在此过程中暴露了您的设计的基本问题!)
在伪代码中你这样做:
如果你进入第6级,那么没有什么可以阅读,所以循环终止,你的程序就在更大的循环中(我怀疑你的条件永远不会退出)。如果你下一个级别,文件中没有该级别的剩余问题,所以同样的事情发生。
相反,您应该将文件读入Question
个对象,并在询问时从中选择。您仍然需要定义不存在的级别6会发生什么,但这是一个要求,而不是一个错误。你还需要让更大的循环更容易扩展,但这似乎又是一个需求问题。
答案 1 :(得分:0)
这是一个快速的伪程序。基本上你想要一个外环就会被击中,只要你退出&#39;一个等级。然后它会确定下一步发送给你的地方。
public void Tester()
{
int level = 1;
while (level < 6)
{
bool direction;
switch (level)
{
case 1:
direction = DoLevel1Stuff();
level += direction ? 1 : -1;
break;
case 2:
direction = DoLevel2Stuff();
level += direction ? 1 : -1;
break;
//etc
}
}
Console.WriteLine("You win!");
}
public bool DoLevel1Stuff()
{
bool didTheyPass;
while (condition)
{
//stuff
}
return didTheyPass;
}
public bool DoLevel2Stuff()
{
}