为什么while循环或if语句的行为不正确?c#
我的节目是测验。问题从第一级问题开始,当用户得到一个正确的问题时,他们进入第二级问题,如果这个问题正确,他们进入第三级问题,依此类推。这是正确的(除了测验开始时,程序在进入第二级之前出于某种原因向用户提出两个第一级问题),但是,主要问题是,当用户答错一个问题时,程序不会进入前一级问题并向他们提出另一个问题,光标在右上角反复闪烁。当他们达到最高级别的问题并得到一个正确答案时,也会发生这种情况(课程必须预期6级问题,但没有6级问题)。这些问题来自一个文本文件,每个级别有10个问题,总共50个问题。问题以字符串结构形式存储在结构包含的文本文件中: 问题编号 问题级别 问题: 问题答案 这就是代码中的问题所在。请帮忙,谢谢为什么while循环或if语句的行为不正确?c#,c#,streamreader,C#,Streamreader,我的节目是测验。问题从第一级问题开始,当用户得到一个正确的问题时,他们进入第二级问题,如果这个问题正确,他们进入第三级问题,依此类推。这是正确的(除了测验开始时,程序在进入第二级之前出于某种原因向用户提出两个第一级问题),但是,主要问题是,当用户答错一个问题时,程序不会进入前一级问题并向他们提出另一个问题,光标在右上角反复闪烁。当他们达到最高级别的问题并得到一个正确答案时,也会发生这种情况(课程必须预期6级问题,但没有6级问题)。这些问题来自一个文本文件,每个级别有10个问题,总共50个问题。问
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";
}
}
}
}
}
静态无效测试(QuestionStruct[]\u测试)
{
布尔问=真;
智力得分=0;
int amounstaked=0;
字符串级别=“1”;
字符串ans;
int pos=1;
var pathToFile=@“.\..\Files\questions.txt”;
使用(StreamReader sr=新的StreamReader(pathToFile,true))
{
而(amounstaked<20 | |得分>=50)
{
Console.Clear();
字符串whatquestionnum=questions[pos].QuestionNum=sr.ReadLine();
string whatlevel=questions[pos].Level=sr.ReadLine();
//第一级问题
while(级别==“1”&&问题[pos]。级别==级别)
{
AmountAsked++;
字符串whatques=questions[pos]。Question=sr.ReadLine();
控制台。写入(whatques);
字符串选择=Console.ReadLine();
字符串决策器=问题[pos]。答案=sr.ReadLine();
如果(选择==决策者)
{
level=“2”;
分数=分数+1;
}
其他的
{
pos++;
}
}
//第2级问题
while(级别==“2”&&问题[pos]。级别==级别)
{
AmountAsked++;
问题[pos]。问题=sr.ReadLine();
Console.Write(问题[pos].Question);
字符串选择2=问题[pos]。答案=sr.ReadLine();
ans=Console.ReadLine();
如果(ans==选择2)
{
级别=“3”;
分数=分数+2;
}
其他的
{
level=“1”;
//问题[pos]。级别==“1”;
}
}
//第三级问题
while(级别==“3”和问题[pos]。级别==级别)
{
AmountAsked++;
问题[pos]。问题=sr.ReadLine();
Console.Write(问题[pos].Question);
字符串选择3=问题[pos]。答案=sr.ReadLine();
ans=Console.ReadLine();
如果(ans==选择3)
{
级别=“4”;
分数=分数+3;
}
其他的
{
level=“2”;
}
}
//四级
while(级别==“4”和问题[pos]。级别==级别)
{
AmountAsked++;
问题[pos]。问题=sr.ReadLine();
Console.Write(问题[pos].Question);
字符串选择4=问题[pos]。答案=sr.ReadLine();
ans=Console.ReadLine();
如果(ans==选择4)
{
级别=“5”;
分数=分数+4;
}
其他的
{
级别=“3”;
}
}
//五级
while(级别==“5”和问题[pos]。级别==级别)
{
AmountAsked++;
问题[pos]。问题=sr.ReadLine();
Console.Write(问题[pos].Question);
字符串选择5=问题[pos]。答案=sr.ReadLine();
ans=Console.ReadLine();
如果(ans==选择5)
{
级别=“5”;
分数=分数+5;
}
其他的
{
级别=“4”;
}
}
}
}
}
这不起作用,因为您只需在文件中循环一次(在这个过程中暴露出设计的基本问题!)
在伪代码中,您可以执行以下操作:
Q
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()
{
}