我如何在涉及数组的C#中编写代码

我如何在涉及数组的C#中编写代码

问题描述:

花了很多时间试图解决没有运气。使用C#



本地驾驶执照办公室要求您创建一个应用程序,对驾驶执照考试的书面部分进行评分。考试有20个多项选择题。以下是正确的答案:



1. B 2. D 3. A 4. A 5. C

6. A 7。 B 8. A 9. C 10. D

11. B 12. C 13. D 14. A 15. D

16. C 17. C 18. B 19. D 20. A

您的程序应将这些正确答案存储在一个数组中。该程序应从文本文件中读取学生对20个问题中每个问题的答案,并将答案存储在另一个数组中。 (创建自己的文本文件以测试应用程序)。在从文件中读取学生的答案后,程序应显示一条消息,指示学生是否通过了考试。 (学生必须正确回答20个问题中的15个才能通过考试。)然后,它应显示正确回答的问题总数,错误回答的问题总数以及显示错误回答问题的问题编号的列表。



我尝试过:



使用系统;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing ;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Windows.Forms;

使用System.IO;



命名空间WernerCh7_4

{

公共部分类Form1:表格

{

public Form1()

{

InitializeComponent ();

}



private void button1_Click(对象发送者, EventArgs e)

{

int score = 0;

int miss = 0;

string [] lines = File.ReadAllLines(Answers / Answers.txt);

string [] correctAnswers = {B,D,A,A,C,A ,B,A,C,D,B,C,D,A,D,C,C,B, D,A};

string [] userAnswers = null;



try

{



if(lines.Length!= 0)

{

userAnswers = lines [0] .Split (new char [] {''});

}



if(userAnswers!= null)

{



List< int32> wrongQuestions = new List< int32>();



for(int index = 0; index< correctAnswers.Length; index ++)

{

if(userAnswers [index] .Equals(correctAnswers [index]))得分++;

else wrongQuestions.Add(index + 1);

}

if(得分> = 15)

{

label1.Text =(通过);

}

其他

{

label1.Text =(失败);


}

label2.Text = score.ToString(N);

miss = 20 - 得分;

label3.Text = missed.ToString(N);



MessageBox.Show(不正确的问题: + string.Join(,,wrongQuestions.ToArray()));



}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

}

have spent multiply hours trying to solve with no luck. using C#

The local driver's license office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers:

1. B 2. D 3. A 4. A 5. C
6. A 7. B 8. A 9. C 10. D
11. B 12. C 13. D 14. A 15. D
16. C 17. C 18. B 19. D 20. A
Your program should store these correct answers in an array. The program should read the student's answers for each of the 20 questions from a text file and store the answers in another array. (Create your own text file to test the application). After a student's answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.

What I have tried:

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

namespace WernerCh7_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int score = 0;
int missed = 0;
string[] lines = File.ReadAllLines("Answers/Answers.txt");
string[] correctAnswers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
string[] userAnswers = null;

try
{

if (lines.Length != 0)
{
userAnswers = lines[0].Split(new char[] { ' ' });
}

if (userAnswers != null)
{

List<int32> wrongQuestions = new List<int32>();

for (int index = 0; index < correctAnswers.Length; index++)
{
if (userAnswers[index].Equals(correctAnswers[index])) score++;
else wrongQuestions.Add(index + 1);
}
if (score >= 15)
{
label1.Text = ("Pass");
}
else
{
label1.Text = ("Fail");

}
label2.Text = score.ToString("N");
missed = 20 - score;
label3.Text = missed.ToString("N");

MessageBox.Show("Incorrect questions: " + string.Join(",", wrongQuestions.ToArray()));

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

引用:

花了很多时间试图解决没有运气。

have spent multiply hours trying to solve with no luck.



编程不是运气问题,而是学习课程和技巧并申请他们。

在代码中陈述你的问题以获得一些帮助。



我们不做你的HomeWork。

HomeWork不会在乞求其他人做你的工作时测试你的技能,它会让你思考并帮助你的老师检查你对你所学课程的理解以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



发展的概念就像这个词所暗示的那样:系统地使用科学和满足特定目标或要求的技术知识。 BusinessDictionary.com [ ^ ]

这与有一个不一样快速谷歌并放弃,如果我找不到正确的代码。


Programming is not a matter of luck, it is a matter of learning lessons and techniques and to apply them.
State your problem in code to get some help.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".


首先打破这个块:编写几个方法来做代码的离散位:阅读学生回答,处理它们,显示结果。

首先写一个读方法 - 你已经得到了它,几乎:

Start by breaking this into chunks: Write a couple of methods which do discrete bits of the code: Read the student answers, process them, display the results.
Start by writing the "read" method - you've got that, pretty much:
private string[] ReadStudent(string path)
    {
    return File.ReadAllLines(path);
    }



然后编写过程方法:


Then write the "process" method:

private int[] ProcessStudent(string[] shouldBe, string[] was)
    {
    if (shouldBe.Length != was.Length)
        {
        throw new ArgumentException("Incorrect number of solutions!");
        }
    ...
    return new int[0];
    }

(我们稍后会考虑填写)

和显示方法:

(We'll look at filling that in in a moment)
And the "display" method:

private void DisplayResults(string[] shouldBe, int[] StudentErrors)
    {
    ...
    }



您使用它们的代码变得更具可读性:


Your code to use them then becomes a lot more readable:

string[] correctAnswers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
string[] studentAnswers = ReadStudent("Answers\\Answers.txt");
int[] studentErrors = ProcessStudent(correctAnswers, studentAnswers);
DisplayResults(correctAnswers, studentErrors);



显示结果可以从两个参数中计算出需要打印的数字:

正确答案的数量是correctAnswers数组的长度减去studentErrors数组的长度。

错误答案的数量是studentErrors数组的长度。

错误的问题编号是在studentErrors数组中。



我会将其作为练习留给读者 - 这很简单!



真正的工作是在ProcessStudent方法中完成的 - 如果您已经介绍了C#Collections,例如List<>,那么它就非常简单了。上课 - 但我会假设你没有,因为如果你知道收藏品,阵列是一个很差的方法来完成整个练习!



所以我们我会用数组做这个。

首先声明一个临时数组来保存错误的答案数字 - 我们会把它变得足够大以容纳一个非常糟糕的学生:


Display results can calculate the numbers it needs to print from just the two parameters:
The number of correct answers is the length of the correctAnswers array, minus the length of the studentErrors array.
The number of incorrect answers is the length of the studentErrors array.
The question numbers that were wrong are in the studentErrors array.

I'll leave that as an exercise for the reader - it's pretty simple!

The "real work" is done in the ProcessStudent method - and it's pretty simple if you have been introduced to C# Collections such as the List<> class - but I'm going to assume you haven't, as arrays are a poor way to do this whole exercise if you are aware of Collections!

So we'll do this with arrays.
Start by declaring a temporary array to hold the wrong answer numbers - we'll make it big enough to hold a really bad student:

int[] wrong = new int[shouldBe.Length];



现在,我们想看看给出的答案,并填写错误的答案。为此我们需要索引:


Now, we want to look at the answers given, and fill out the wrong answers. For that we need to indexes:

int[] wrong = new int[shouldBe.Length];
int wrongIndex = 0;
for (int i = 0; i < shouldBe.Length; i++)
    {
    //...
    }

在循环内部它也非常简单:检查答案,如果错误,添加数字到错误数组:

Inside the loop it's pretty simple as well: check the answer, and if it's wrong, add the number to the errors array:

if (shouldBe[i] != was[i])
    {
    wrong[wrongIndex++] = i + 1;
    }

索引是从零开始的,所以我们需要添加一个来获得一个基于一个问题的数字。

现在我们需要做的就是返回只有错误数组中的错误问题编号:

The index is zero-based, so we need to add one to get a one-based question number.
Now all we need to do is return only the error question numbers in the "wrong" array:

int[] justTheWrongOnes = new int[wrongIndex];
for (int i = 0; i < wrongIndex; i++)
    {
    justTheWrongOnes[i] = wrong[i];
    }
return justTheWrongOnes;

我们已经完成了!



但是,我建议您阅读并理解这一点,而不是将其复制到您的代码中并将其交给您:如果您完全按照我的说法进行操作,那么您的导师会知道您不写它...所以读它,理解它,并想一个你自己可以编写的类似解决方案。

And we're done!

However, I'd suggest that you read and understand this rather than copy'n'paste it into your code and hand it in: if you hand that in exactly as I wrote it, your tutor will know that you didn't write it...so read it, understand it, and think of a similar solution that you can write yourself.