如何使代码在返回某些东西之前运行一定次数
本文关键字:运行 代码 何使 返回 | 更新日期: 2023-09-27 18:07:49
我制作了一款益智游戏,我需要创建一个方法(SuccessOrFail)来返回用户是否通关。
namespace D4
{
/// <summary>
/// Displays the trivia and returns whether the user succeeded or not, number of questions asked, and a free piece of trivia.
/// </summary>
public partial class TriviaForm : Form
{
private Trivia trivia;
private Question question;
private Random rand = new Random();
private HashSet<int> pickedQuestion = new HashSet<int>();
private string usersAnswer;
private int numCorrectAnswers;
private int numIncorrectAnswers;
public TriviaForm()
{
InitializeComponent();
this.trivia = new Trivia();
QuestionRandomizer();
QuestionOutputter();
}
/// <summary>
/// This method will return true if succeeded or false if not.
/// </summary>
/// <returns>Whether the user got the trivia right or not</returns>
public bool SuccessOrFail(bool wumpus)
{
bool successOrFail = false;
int maxQuestions = 3;
if (wumpus == true)
maxQuestions = 5;
int numNeededCorrect = maxQuestions / 2 + 1;
if (this.usersAnswer == question.CorrectAnswer.ToString())
numCorrectAnswers++;
else
numIncorrectAnswers++;
if (numCorrectAnswers + numIncorrectAnswers == maxQuestions)
{
if (numCorrectAnswers == numNeededCorrect)
successOrFail = true;
else
successOrFail = false;
numCorrectAnswers = 0;
numIncorrectAnswers = 0;
return successOrFail;
}
else
return false;
}
/// <summary>
/// This method will output a free answer to the player.
/// </summary>
public string FreeTrivia()
{
return question.Freetrivia;
}
// This method tells the player whether they were correct or not.
private void CorrectOrNot()
{
if (this.usersAnswer == question.CorrectAnswer.ToString())
MessageBox.Show("Correct");
else
MessageBox.Show("Incorrect");
}
// Displays the questions and answers on the form.
private void QuestionOutputter()
{
this.txtQuestion.Text = question.QuestionText;
this.txtBox0.Text = question.Answers[0];
this.txtBox1.Text = question.Answers[1];
this.txtBox2.Text = question.Answers[2];
this.txtBox3.Text = question.Answers[3];
}
// Clears the TextBoxes and displays a new random question.
private void btnNext_Click(object sender, EventArgs e)
{
this.usersAnswer = txtAnswer.Text;
CorrectOrNot();
this.txtQuestion.Clear();
this.txtBox0.Clear();
this.txtBox1.Clear();
this.txtBox2.Clear();
this.txtBox3.Clear();
this.txtAnswer.Clear();
this.txtAnswer.Focus();
QuestionRandomizer();
QuestionOutputter();
this.txtsuc.Text = SuccessOrFail(false).ToString();
}
// Choose a random number and assign the corresponding data to question, refreshes the list if all questions used.
private void QuestionRandomizer()
{
if (pickedQuestion.Count < trivia.AllQuestions.Count)
{
int random;
do
{
random = rand.Next(trivia.AllQuestions.Count);
} while (pickedQuestion.Contains(random));
pickedQuestion.Add(random);
this.question = trivia.AllQuestions.ToArray()[random];
if (pickedQuestion.Count == trivia.AllQuestions.ToArray().Length)
pickedQuestion.Clear();
}
}
}
}
我的问题是如何使它使代码问用户3或5个问题,然后返回用户是否赢了?
我想知道如果以某种方式我可以使一个公共void,只是使表单弹出并问用户3到5个问题,然后一旦它问问题的最大数量,关闭,然后有一个方法,返回true如果用户赢了,或false如果他们没有。但我真的不知道该怎么做。
编辑:所以我知道for循环可以使代码运行不止一次。但问题是,我有是,我不知道如何使它,所以琐事游戏问3到5个问题之前返回的东西。
EditAgain:所以我已经尝试了一些像你说的(我认为),其中我有稍微不同的代码…
namespace D4
{
/// <summary>
/// Displays the trivia and returns whether the user succeeded or not, number of questions asked, and a free piece of trivia.
/// </summary>
public partial class TriviaForm : Form
{
private Trivia trivia;
private Question question;
private Map map;
private Random rand = new Random();
private HashSet<int> pickedQuestion = new HashSet<int>();
private string usersAnswer;
private int numCorrectAnswers;
private int numIncorrectAnswers;
private bool successOrFail;
public TriviaForm()
{
InitializeComponent();
this.trivia = new Trivia();
this.map = new Map();
QuestionRandomizer();
QuestionOutputter();
}
/// <summary>
/// This method will return true if succeeded or false if not.
/// </summary>
/// <returns>Whether the user got the trivia right or not</returns>
public bool SuccessOrFail
{
get { return this.successOrFail; } }
}
/// <summary>
/// This method will output a free answer to the player.
/// </summary>
public string FreeTrivia()
{
return question.Freetrivia;
}
// This method tells the player whether they were correct or not.
private void CorrectOrNot()
{
if (this.usersAnswer == question.CorrectAnswer.ToString())
MessageBox.Show("Correct");
else
MessageBox.Show("Incorrect");
}
// Displays the questions and answers on the form.
private void QuestionOutputter()
{
this.txtQuestion.Text = question.QuestionText;
this.txtBox0.Text = question.Answers[0];
this.txtBox1.Text = question.Answers[1];
this.txtBox2.Text = question.Answers[2];
this.txtBox3.Text = question.Answers[3];
}
// Clears the TextBoxes and displays a new random question.
private void btnNext_Click(object sender, EventArgs e)
{
this.usersAnswer = txtAnswer.Text;
// The max number of questions that can be asked.
int maxQuestions = 3;
if (map.wumpus == true)
maxQuestions = 5;
// The number of questions needed to be answered correctly for the user to win.
int numNeededCorrect = maxQuestions / 2 + 1;
if (this.usersAnswer == question.CorrectAnswer.ToString())
numCorrectAnswers++;
else
numIncorrectAnswers++;
if (numCorrectAnswers + numIncorrectAnswers == maxQuestions)
{
if (numCorrectAnswers == numNeededCorrect)
this.successOrFail = true;
else
this.successOrFail = false;
numCorrectAnswers = 0;
numIncorrectAnswers = 0;
// Somehow close the form.
}
CorrectOrNot();
this.txtQuestion.Clear();
this.txtBox0.Clear();
this.txtBox1.Clear();
this.txtBox2.Clear();
this.txtBox3.Clear();
this.txtAnswer.Clear();
this.txtAnswer.Focus();
QuestionRandomizer();
QuestionOutputter();
this.txtsuc.Text = SuccessOrFail(false).ToString();
}
// Choose a random number and assign the corresponding data to question, refreshes the list if all questions used.
private void QuestionRandomizer()
{
if (pickedQuestion.Count < trivia.AllQuestions.Count)
{
int random;
do
{
random = rand.Next(trivia.AllQuestions.Count);
} while (pickedQuestion.Contains(random));
pickedQuestion.Add(random);
this.question = trivia.AllQuestions.ToArray()[random];
if (pickedQuestion.Count == trivia.AllQuestions.ToArray().Length)
pickedQuestion.Clear();
}
}
}
}
所以这段代码的主要变化只是新的属性(SuccessOrFail),它现在返回用户是否赢了,通过获得SuccessOrFail变量。该变量在btnNext_Click中被调用,其中我使用正确/不正确答案的数量来查看他们是否回答了最大数量的问题,如果他们这样做并且他们有所需的正确答案数量,那么successOrFail变量= true,否则,它为false。然而,当我这样做时,SuccessOrFail属性仍然返回初始值false。因为在开始时,没有设置任何内容。
我对这段代码的问题是:这比之前的代码好吗?是否有办法修改这段代码,使其只能在回答所有问题后返回SuccessOrFail ?
假设从你的问题中你有3-5个问题之间的随机问题。你所需要做的就是问一个问题,一旦回答就返回一个布尔值。如果有正确的布尔值,则返回获胜的游戏状态。
首先,您需要一个私有成员变量来跟踪已经问了多少个问题,如下所示:
private int questionsAsked = 0;
然后在btnNext_Click
处理程序中,您可以增加这个总数,然后检查他们是否回答了足够的问题,以便在需要时关闭表单:
questionsAsked++;
if (questionsAsked >= 5) this.Hide(); // this will hide the form but keep the object so you can fetch the results after.
最后,要从表单中获得结果,只需声明一个公共属性以返回一个布尔值来表示用户是否获胜。您没有提到是什么定义了赢家,因此需要添加决定这一点的表达式:
public bool IsWinner {
get {
return /* true if winner */;
}
}
我不确定你有什么应用程序框架围绕你的表单,但你应该有一个引用的表单对象,因此可以得到这样的结果:
var winner = MyTriviaForm.IsWinner;