C# 如何将用户返回到前一个问题
如前所述,我是一个编程新手,我开始练习做一个小测验程序 如果人们回答错了,我试图让他们回到同一个问题上来。我试着把它放在一个while循环中,让while循环一直运行到正确的答案完成为止。但是当我执行while循环时,我不能指向包含用户输入的“ask”变量。如果这有道理,我希望你能帮忙:) 这是我的密码:C# 如何将用户返回到前一个问题,c#,C#,如前所述,我是一个编程新手,我开始练习做一个小测验程序 如果人们回答错了,我试图让他们回到同一个问题上来。我试着把它放在一个while循环中,让while循环一直运行到正确的答案完成为止。但是当我执行while循环时,我不能指向包含用户输入的“ask”变量。如果这有道理,我希望你能帮忙:) 这是我的密码: using System; namespace Quiz1 { class Program { /// <summary> ///
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!");
}
}
}
}
}
使用系统;
命名空间Quiz1
{
班级计划
{
///
///从命令提示符读取并尝试转换为整数。
///
///返回从命令提示符读取的转换后的整数值,如果不成功,则返回零。
///
私有静态int ReadNumber()
{
string text=Console.ReadLine();
if(int.TryParse(text,out int voresTal)==true)
{
返回沃雷斯塔尔;
}
返回0;
}
///
///请求用户输入他们的年龄,使用minAge和maxAge作为年龄下限和上限。
///如果检测到不正确的输入,将向用户提供适当的反馈。
///
///最低可接受年龄。
///可接受的最大年龄。
///以整数值形式返回minAge和maxAge范围内的有效年龄。
私有静态整型读取(整型最小值,整型最大值)
{
int age=minAge-1;
而(年龄maxAge)
{
控制台。写线(“Indtast din Alder”);
年龄=阅读人数();
如果(年龄==0)
{
//吉弗·布鲁格伦在汉费雷德围攻om。
Console.WriteLine(“输入5-120之间的有效年龄”);
}
else if(年龄最大年龄)
{
WriteLine(“你不可能:“+age+”old”);
}
}
回归年龄;
}
私有静态int AskQuestion(字符串问题,字符串[]答案)
{
int-choice=-1;
while(选项==-1)
{
控制台。写线(问题);
for(int i=0;i 如果(输入>=1&&input这不是while循环所做的:
当用户给出介于1和3之间的输入(答案数组的长度)时,while循环将停止
这意味着您的while循环只有在用户输入5或0时才会再次执行
但你就快到了。
如果你想让你的测验重复答案,只需将你的主要功能修改为
int-theRightAnswer=0;
int ask;
做{
问(
“地球是圆的还是平的?”,
答案
);
Console.WriteLine(“你的答案:+答案[询问]);
如果(询问==正确答案)
{
Console.WriteLine(“恭喜你答对了!”);
}
其他的
Console.WriteLine(“重试”);
}while(问!=正确答案);
这不是while循环所做的:
当用户给出介于1和3之间的输入(答案数组的长度)时,while循环将停止
这意味着您的while循环只有在用户输入5或0时才会再次执行
但你就快到了。
如果你想让你的测验重复答案,只需将你的主要功能修改为
int-theRightAnswer=0;
int ask;
做{
问(
“地球是圆的还是平的?”,
答案
);
Console.WriteLine(“你的答案:+答案[询问]);
如果(询问==正确答案)
{
Console.WriteLine(“恭喜你答对了!”);
}
其他的
Console.WriteLine(“重试”);
}while(问!=正确答案);
尝试简单地修改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;
}
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
);
}
}
私有静态int-AskQuestion(字符串问题,字符串[]答案,int-right)
{
int-choice=-1;
bool-isRight=false;
而(!isRight)
{
控制台。写线(问题);
for(int i=0;i 如果(input>=1&&input尝试简单地修改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;
}
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
);
}
}
私有静态int-AskQuestion(字符串问题,字符串[]答案,int-right)
{
int-choice=-1;
bool-isRight=false;
而(!isRight)
{
控制台。写线(问题);
for(int i=0;i 如果(input>=1&&input我不确定问题是,在ReadAge中,访问年龄应该没有问题。我不确定问题是,在ReadAge中,访问年龄应该没有问题。如果使用这种方法,请确保将函数从AskQuestion重命名为AskQuestionUntilCorrect,因为这是一种很好的做法OUF