gpt4 book ai didi

c# - 变量不会在我的代码中递增/递减

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:39 29 4
gpt4 key购买 nike

if (getchar == '+') {
answer = getnum1+getnum2; // if the random operation is add, it will add
addtemp++; // <---- disregard this
if (answer == getanswer) // the answer from my textbox which is
{ // user-input it is stored on "getanswer"
correct++; // it is compared if its correct or wrong
addcomp++;
}
else { wrong++; }
}
else if (getchar == '-') {
subtemp++;
answer = nextValue - nextValue1;
if (answer == getanswer) {
correct++;
subcomp++;
}
else { wrong++; }
}
else if (getchar == '*') {
multemp++;
answer = nextValue * nextValue1;
if (answer == getanswer) {
correct++;
mulcomp++;
}
else { wrong++; }
}
else if (getchar == '/') {
divtemp++;
answer = nextValue / nextValue1;
if (answer == getanswer) {
correct++;
divcomp++;
}
else { wrong++; }
}
else if (getchar == '%') {
modtemp++;
answer = nextValue % nextValue1;
if (answer == getanswer) {
correct++;
modcomp++;
}
else { wrong++; }
}

C# 编程帮助!现在每当我按下“分数”按钮时,它就是一个 MessageBox.Show(correct or wrong),值是错误的。它有时会增加校正但只有一两次。我的代码有问题吗?

////////////////////////////////////////////////////////@boncodigo 的完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace A1_ALS_Noroña
{
public partial class Form1 : Form
{
int minV=0, maxV=0,ques=0,tempques=1;

int addtemp, subtemp, multemp, divtemp, modtemp;
int addcomp, subcomp, mulcomp, divcomp, modcomp;
int answer,getanswer;
int getnum1, getnum2;

char getchar;
char[] select = new char[5];
int count=0;
int correct, wrong;

public Form1()
{
InitializeComponent();
}



private void bttnstart_Click(object sender, EventArgs e)
{
bttnanswer.Enabled = true;
grpbox1.Enabled = false;
bttnanswer.Enabled = true;
lblnum1.Visible = true;
lblnum2.Visible = true;
lbloperator.Visible = true;
bttnstop.Enabled = true;
bttnscore.Enabled = true;
bttnstart.Enabled = false;


Random random = new Random();
int nextValue = random.Next(minV, maxV);
int nextValue1 = random.Next(minV, maxV);
lblnum1.Text = nextValue.ToString();
lblnum2.Text = nextValue1.ToString();

var rand = new Random();

char num = select[rand.Next(count)];
lbloperator.Text = Convert.ToString(num);
}

private void txtboxmin_TextChanged(object sender, EventArgs e)
{
minV = Convert.ToInt32(txtboxmin.Text);
}

private void txtbxmax_TextChanged(object sender, EventArgs e)
{
maxV = Convert.ToInt32(txtbxmax.Text);
}

private void bttnexit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void bttnstop_Click(object sender, EventArgs e)
{
MessageBox.Show("APPLICATION STOP! The application will restart.");
Application.Restart();
}



private void bttnanswer_Click(object sender, EventArgs e)
{

tempques++;
Random random = new Random();
int nextValue = random.Next(minV,maxV);
int nextValue1 = random.Next(minV, maxV);
lblnum1.Text = nextValue.ToString();

var rand = new Random();
char num = select[rand.Next(count)];
lbloperator.Text = Convert.ToString(num);
lblnum2.Text = nextValue1.ToString();
getnum1 = Convert.ToInt32(lblnum1.Text);
getnum2 = Convert.ToInt32(lblnum2.Text);
getanswer = Convert.ToInt32(txtbxans.Text);
getchar = Convert.ToChar(lbloperator.Text);

if (getchar == '+')
{

answer = getnum1 + getnum2;
addtemp++;
if (answer == getanswer)
{
correct++;
addcomp++;
}
else
{
wrong++;
}
}
else if (getchar == '-')
{
subtemp++;
answer = nextValue - nextValue1;
if (answer == getanswer)
{
correct++;
subcomp++;
}
else
{
wrong++;
}
}
else if (getchar == '*')
{
multemp++;
answer = nextValue * nextValue1;
if (answer == getanswer)
{
correct++;
mulcomp++;
}
else
{
wrong++;
}
}
else if (getchar == '/')
{
divtemp++;
answer = nextValue / nextValue1;
if (answer == getanswer)
{
correct++;
divcomp++;
}
else
{
wrong++;
}
}
else if (getchar == '%')
{
modtemp++;
answer = nextValue % nextValue1;
if (answer == getanswer)
{
correct++;
modcomp++;
}
else
{
wrong++;
}
}







}

private void txtbxques_TextChanged(object sender, EventArgs e)
{
ques = Convert.ToInt32(txtbxques.Text);

}

private void chkbxtimer_CheckedChanged(object sender, EventArgs e)
{
rdoeasy.Enabled = true;
rdomed.Enabled = true;
rdohard.Enabled = true;
}

private void chkboxAdd_CheckedChanged(object sender, EventArgs e)
{

if (chkboxAdd.Checked == true)
{
select[count] = '+';
count++;
}
else if (chkboxAdd.Checked == false)
{
Array.Clear(select, 0, select.Length);
count--;
}

}

private void chkboxSub_CheckedChanged(object sender, EventArgs e)
{
if (chkboxSub.Checked == true)
{
select[count] = '-';
count++;

}
else if (chkboxSub.Checked == false)
{
Array.Clear(select, 0, select.Length);
count--;
}

}

private void chkboxMul_CheckedChanged(object sender, EventArgs e)
{
if (chkboxMul.Checked == true)
{
select[count] = '*';
count++;

}
else if (chkboxMul.Checked == false)
{
Array.Clear(select, 0, select.Length);
count--;
}

}

private void chkboxDiv_CheckedChanged(object sender, EventArgs e)
{
if (chkboxDiv.Checked == true)
{
select[count] = '/';
count++;

}
else if (chkboxDiv.Checked == false)
{
Array.Clear(select, 0, select.Length);
count--;
}
}

private void chkboxMod_CheckedChanged(object sender, EventArgs e)
{
if (chkboxMod.Checked == true)
{
select[count] = '%';
count++;

}
else if (chkboxMod.Checked == false)
{
Array.Clear(select, 0, select.Length);
count--;
}
}

private void bttnscore_Click(object sender, EventArgs e)
{
MessageBox.Show("Correct Answer"+correct);
}




}
}

最佳答案

提前一件事:我不知道你的错误在哪里。这里只是一些提示,我认为如果您考虑它们会很有意义,以避免将来出现类似的错误:

如果我必须检查这段代码,我的主要问题是重复代码的数量,其中非常相似的多行长模式被一遍又一遍地复制。我认为在整个代码中你不调用任何方法,而是立即在事件处理程序中实现一些东西,从而重复你自己并增加错误的可能性。让我们看一下这段代码:

if (chkboxSub.Checked == true)
{
select[count] = '-';
count++;

}
else if (chkboxSub.Checked == false)
{
Array.Clear(select, 0, select.Length);
count--;
}

除了将多个运算符添加到选择数组时的计数错误外,此代码会重复多次。让我们将代码提取到一个方法中,并使更改的位可参数化:

void AddOrRemoveOperator(bool isChecked, char operatorChar) {
if (isChecked) {
select[count] = operatorChar;
count++;
}
else {
Array.Clear(select, 0, select.Length);
count--;
}
}

现在您可以多次调用该方法,例如喜欢:

AddOrRemoveOperator(chkboxSub.Checked, '-');

下一点是缺乏 .NET 基类库知识 (BCL)。例如,使用 List<T> 不是更容易吗?而不是数组?

上述方法变为:

void AddOrRemoveOperator(bool isChecked, char operatorChar) {
if (isChecked) {
select.Add(operatorChar);
}
else {
select.Clear();
}
}

观察:除加法运算符外,所有运算符都使用值 nextValue、nextValue1,而加法运算符使用 getnum1 和 2。这是故意的吗?

缺少在 bttnanswer_Click 中提取代码块到他们自己的类中,你也可以将重复的代码提取到一个方法中:

 void PerformComparison(Func<int> answerProvider, 
ref int operatorCount,
ref int operatorSpecificCorrectCount)
{
var answer = answerProvider();
operatorCount++;
if (answer == getanswer) {
correct++;
operatorSpecificCorrectCount++;
}
else {
wrong++;
}
}

那段代码仍然会让我抓狂(因为你编写的类缺乏凝聚力),但我们已经与代码重复作斗争。现在您可以调用该方法,例如像那样:

if (getchar == '+')
{
PerformComparison(()=>getnum1 + getnum2, ref addtemp, ref addcomp);
}

有很多技术可以将代码变形为更易于测试和维护的形式(重构),到目前为止我们只使用了提取方法。有关更多技术,请参阅本书 Refactoring: Improving the Design of Existing Code仍然非常值得推荐。

关于c# - 变量不会在我的代码中递增/递减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13551345/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com