所以正如我所说的,我是一个完全的编程新手,我从做一个小测试程序开始我的练习。
如果人们答错了,我试着让他们回到同一个问题上来。我试着把它全部放在一个while循环中,让while循环运行,直到得到正确的答案。但是当我执行while循环时,我不能指向包含用户输入的“ask”变量。如果这有意义,我希望你能帮忙:)
下面是我的代码:
using System;
namespace Quiz1
{
class Program
{
/// <summary>
/// Reads from the command prompt and attempts to convert into an integer.
/// </summary>
/// <returns>Returns the converted integer value read from the command prompt, or zero if unsuccessful.</returns>
///
private static int ReadNumber()
{
string text = Console.ReadLine();
if (int.TryParse(text, out int voresTal) == true)
{
return voresTal;
}
return 0;
}
/// <summary>
/// Requests the user to enter their age, using minAge and maxAge as lower and upper age limits.
/// If incorrect input is detected, proper feedback is provided to the user.
/// </summary>
/// <param name="minAge">The minimum acceptable age.</param>
/// <param name="maxAge">The maximum acceptable age.</param>
/// <returns>Returns a valid age within rage of minAge and maxAge, as an integer value.</returns>
private static int ReadAge(int minAge, int maxAge)
{
int age = minAge - 1;
while (age < minAge || age > maxAge)
{
Console.WriteLine("Indtast din Alder.");
age = ReadNumber();
if (age == 0)
{
//Giver brugeren besked om at han fejlede.
Console.WriteLine("Input a Valid Age between 5-120");
}
else if (age < minAge || age > maxAge)
{
Console.WriteLine("Impossible for you to be: " + age + " old ");
}
}
return age;
}
private static int AskQuestion(string question, string[] answers)
{
int choice = -1;
while (choice == -1)
{
Console.WriteLine(question);
for (int i = 0; i < answers.Length; i++)
{
int choiceNumber = i + 1;
Console.WriteLine(choiceNumber + ": " + answers[i]);
}
Console.WriteLine("Your Choice: ");
int input = ReadNumber();
if (input >= 1 && input <= answers.Length)
{
choice = input - 1;
}
}
return choice;
}
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Allround quiz for your enjoyment!");
Console.WriteLine("Lets start by knowing your age to determine the Difficulty");
int age = ReadAge(5, 120);
Console.WriteLine("You are: " + age + "old");
Console.WriteLine("Thank you, i now know your age and will determine the Difficulty. Good luck !");
if (age <= 16)
{
string[] answers = new string[]
{
"yes",
"no",
"none of the above"
};
int theRightAnswer = 0;
int ask = AskQuestion(
"Is the earth round or flat ?",
answers
);
Console.WriteLine("Your answer: " + answers[ask]);
if (ask == theRightAnswer)
{
Console.WriteLine("Congratz your correct!");
}
}
}
}
}
尝试简单地修改AskQuestion方法,使用户在给出正确答案之前不允许退出,如下所示:
private static int AskQuestion(string question, string[] answers, int right)
{
int choice = -1;
bool isRight = false;
while (!isRight)
{
Console.WriteLine(question);
for (int i = 0; i < answers.Length; i++)
{
int choiceNumber = i + 1;
Console.WriteLine(choiceNumber + ": " + answers[i]);
}
Console.WriteLine("Your Choice: ");
int input = ReadNumber();
if (input >= 1 && input <= answers.Length)
{
choice = input - 1;
}
Console.WriteLine("Your answer: " + answers[choice]);
if (choice == right)
{
Console.WriteLine("Congratz your correct!");
isRight = true;
} else{
Console.WriteLine("Try again!");
}
}
return choice;
}
然后像这样调用methood:
public static void Main(string[] args)
{
Console.WriteLine("Welcome to the Allround quiz for your enjoyment!");
Console.WriteLine("Lets start by knowing your age to determine the Difficulty");
int age = ReadAge(5, 120);
Console.WriteLine("You are: " + age + "old");
Console.WriteLine("Thank you, i now know your age and will determine the Difficulty. Good luck !");
if (age <= 16)
{
string[] answers = new string[]
{
"yes",
"no",
"none of the above"
};
int theRightAnswer = 0;
AskQuestion(
"Is the earth round or flat ?",
answers,
theRightAnswer
);
}
}
这不是while循环正在执行的操作:
您的while循环将在用户给出介于1和3(答案数组的长度)之间的输入时停止
这意味着只有当用户给出像5或0这样的输入时,您的while循环才会再次执行。
但您已经差不多了。
如果您希望测验重复答案,只需将main函数修改为以下内容
int theRightAnswer = 0;
int ask;
do {
ask = AskQuestion(
"Is the earth round or flat ?",
answers
);
Console.WriteLine("Your answer: " + answers[ask]);
if (ask == theRightAnswer)
{
Console.WriteLine("Congratz your correct!");
}
else
Console.WriteLine("try again");
} while(ask != theRightAnswer);