C# 比较两个数组,同时计算正确答案和错误答案的数量
我编写了一个程序,用户在其中玩游戏并猜测答案。 除了我的数组外,其他一切都很好,这个程序有两个数组,第一个是usersAnswers,它包含用户的选择,第二个是decompetimearray,它包含正确的答案。我写了一个方法,程序将比较两个数组,计算用户得到正确和错误答案的数量,并将它们的值放在两个单独的标签中。该程序运行良好,但它总是给我0为正确和不正确的标签,如果你能帮助我解决这个小问题,并使程序正确计算答案,我将不胜感激。这是我的密码:C# 比较两个数组,同时计算正确答案和错误答案的数量,c#,arrays,C#,Arrays,我编写了一个程序,用户在其中玩游戏并猜测答案。 除了我的数组外,其他一切都很好,这个程序有两个数组,第一个是usersAnswers,它包含用户的选择,第二个是decompetimearray,它包含正确的答案。我写了一个方法,程序将比较两个数组,计算用户得到正确和错误答案的数量,并将它们的值放在两个单独的标签中。该程序运行良好,但它总是给我0为正确和不正确的标签,如果你能帮助我解决这个小问题,并使程序正确计算答案,我将不胜感激。这是我的密码: public void usersAnswers(
public void usersAnswers()
{
userAnswers[0] = newspaperLbl.Text;
userAnswers[1] = aluminumCanLbl.Text;
userAnswers[2] = glassBottleLbl.Text;
userAnswers[3] = plasticbagLbl.Text;
userAnswers[4] = cupLbl.Text;
}
public void correctAnswers()
{
decompTimeArray[0] = "6 Weeks";
decompTimeArray[1] = "10-20 Years";
decompTimeArray[2] = "80-200 Years";
decompTimeArray[3] = "1,000,000 Years";
decompTimeArray[4] = "Never";
}
public void compareArrays()
{
bool arraysEqual = true;
int index;
if (userAnswers.Length != decompTimeArray.Length)
{
arraysEqual = false;
}
for (index = 0; index < userAnswers.Length; index++)
{
if (decompTimeArray[index] != userAnswers[index])
{
arraysEqual = false;
wrong++;
}
else
{
arraysEqual = false;
right++;
}
/*This part of the program will compare the arrays from
* methods 1,2 we use a for loop*/
}
if (arraysEqual)
{
Results Result = new Results();
Result.correctAnswersLbl.Text = right.ToString("n");
}
else
{
Results Result = new Results();
Result.incorrectAnswersLbl.Text = wrong.ToString("n");
}
}
public void checkAnswersBtn_Click(object sender, EventArgs e)
{
Results Result = new Results();
Result.userAnswer1Label.Text = newspaperLbl.Text;
Result.userAnswer2Label.Text = aluminumCanLbl.Text;
Result.userAnswer3Label.Text = glassBottleLbl.Text;
Result.userAnswer4Label.Text = plasticbagLbl.Text;
Result.userAnswer5Label.Text = cupLbl.Text;
Result.correctAnswersLbl.Text = right.ToString("n");
Result.incorrectAnswersLbl.Text = wrong.ToString("n");
percentage = (wrong / 5) * 100;
Result.percentageLbl.Text = percentage.ToString("p");
this.Hide();
Introduction Intro = new Introduction();
Intro.Hide();
Result.ShowDialog();
}
}
}如果错误是一个整数,您在这里进行整数除法:百分比=错误/5*100
例如,如果错的是4,你会得到4/5=0*100=0。因此,您将始终显示0
您需要将错误转换为双精度、十进制或浮点,或者将值5本身转换为更可取的值,因为您无法通过将其重写为5.0来获得“部分”答案
所以
将触发正确的除法类型;操作现在将计算为4/5.0=0.8*100=80考虑调试代码。旁注:重复的数据总是令人痛苦的。您确实不需要错误地计算arraysEqual变量-在正常情况下,right+Error应该是数组的长度。。。
percentage = (int)((wrong / 5.0) * 100);
^
|
This will be a double-|