所以我对编程尚不熟悉,所以我不知道该如何进行这项工作,我希望每次用户获得正确或错误答案时,切换情况都循环到新问题。我不确定我是否正确路径上的m将是很高兴有人可以教我。请随时纠正我,请原谅我的英语。
int ans;
string sign;
int score = 0;
public timedchallenge()
{
InitializeComponent();
}
void Timedchallenge_Load(object sender, EventArgs e)
{
Random rand = new Random();
int num1 = rand.Next(1, 31);
int num2 = rand.Next(1, 31);
int cycles = rand.Next(1, 4);
{
switch (cycles)
{
case 1:
ans = num1 + num2;
sign = "+";
break;
case 2:
ans = num1 - num1;
sign = "-";
break;
case 3:
ans = num1 * num2;
sign = "*";
break;
default:
ans = 0;
sign = "error ";
break;
}
}
label2.Text = num1.ToString();
label3.Text = sign;
label4.Text = num2.ToString();
RandomWelcomeMessages myRandomWelcomeMessages = new RandomWelcomeMessages();
String msg = myRandomWelcomeMessages.GetRandom();
labelmsgs.Text = msg + " " + typeofquiz.username;
}
private void ButtonSubmit_Click(object sender, EventArgs e)
{
if (textBoxAns.Text == ans.ToString())
{
score++;
textBoxAns.Text = "";
labelScore.Text = score.ToString();
}
else
{
textBoxAns.Text = "";
}
}
您没有确切地发布RandomWelcomeMessages
类的实际作用,所以我假设这只是程序启动时要做的一件事。
我首先会提供一种方法来生成一个新问题,并在适当的地方调用它。将其封装在自己的类中可能是值得的。
int ans;
string sign;
int score = 0;
public timedchallenge()
{
InitializeComponent();
}
void Timedchallenge_Load(object sender, EventArgs e)
{
RandomWelcomeMessages myRandomWelcomeMessages = new RandomWelcomeMessages();
String msg = myRandomWelcomeMessages.GetRandom();
labelmsgs.Text = msg + " " + typeofquiz.username;
AdvanceQuestion();
}
private void ButtonSubmit_Click(object sender, EventArgs e)
{
bool answerCorrect = textBoxAns.Text == ans.ToString();
if (answerCorrect)
labelScore.Text = ++score.ToString();
AdvanceQuestion();
}
private void AdvanceQuestion() {
Random rand = new Random();
int num1 = rand.Next(1, 31);
int num2 = rand.Next(1, 31);
int cycles = rand.Next(1, 4);
switch (cycles)
{
case 1:
ans = num1 + num2;
sign = "+";
break;
case 2:
ans = num1 - num1;
sign = "-";
break;
case 3:
ans = num1 * num2;
sign = "*";
break;
default:
// you should probably throw an exception here
ans = 0;
sign = "error ";
break;
}
label2.Text = num1.ToString();
label3.Text = sign;
label4.Text = num2.ToString();
textBoxAns.Text = "";
}