帮助我的代码进行分配 [英] Help with my code for an assignment

查看:61
本文介绍了帮助我的代码进行分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Yo,正在进行C#任务,我的朋友和我被困住了。我们正在制作一个虚假的scantron程序,它使用StreamWriter和StreamReader来制作一个exam.txt文件,我们引用这些文件来假想学生的输出标记。我们尝试运行它,它说

Yo, working on a C# assignment and my friends and I are stuck. We're making a fake scantron program that uses StreamWriter and StreamReader to make an exam.txt file that we reference to output marks of imaginary students. We try running it and it says that on the


if(students [key] [j] == answers [ j])

if(students[key][j]==answers[j])


索引超出了数组的范围。我们哪里出错?

The index is outside of the bounds of the array. Where are we going wrong?


以下是完整代码,仅供参考:

Here is the full code, for reference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamWriter writer =
            new StreamWriter("exam.txt"))
            {
                writer.WriteLine("BEDEEEACDEDDEEDBBAAD");
                writer.WriteLine("2956 EBCDEEADDCCXBBAXXDBC");
                writer.WriteLine("1957 BBAEECAXCCEXEAADADDB");
                writer.WriteLine("3309 EACECXBDBEEXDBAEBBAX");
                writer.WriteLine("9573 DEECBABEDAEEAXDEBXDA");
                writer.WriteLine("4677 AXCEDCXEACDDXECCCEDC");
                writer.WriteLine("9274 EXECXEXEXCBCAXADDBBB");
                writer.WriteLine("3746 XBBAEDBCXCEDCEDXXXXA");
                writer.WriteLine("1966 EXCEBBBEDXXXAXDXXBXE");
                writer.WriteLine("9922 XXCXACEBACXAAXXEDEDB");
                writer.WriteLine("2222 DBDAXBXECACBDAEDBBXD");
                writer.WriteLine("0");
                writer.Close();


            }

            using (StreamReader reader = new StreamReader("exam.txt"))
            {
                int correct = 0;
                string answer;
                string currentLine;
                string[] idAnswerPair;
                answer = reader.ReadLine();
                Dictionary<string, string> students = new Dictionary<string, string>();
                int[] answerKey = new int[20];
                while (!reader.EndOfStream)
                {
                    currentLine = reader.ReadLine();
                    idAnswerPair = currentLine.Split(' ');
                    students.Add(idAnswerPair[0], idAnswerPair[0]);


                }
                foreach (KeyValuePair<String, String> pair in students)
                {
                    Console.WriteLine($"{pair.Key}: {pair.Value}");
                }

                char[] answers = answer.ToCharArray();
                foreach (string key in students.Keys)
                {
                    for(int j = 0; j < 20; j++)
                    {
                        if(students[key][j]==answers[j])
                        {
                            correct = correct + 4;
                        }
                        else if(students[key][j] != answers[j])
                        {
                            correct = correct - 1;
                        }
                        else
                        {
                            correct = correct + 0;
                        }
                    }
                }
                /*foreach (char a in answers)
                {
                    for(int i = 0; student1.Equals(answer); i++)
                    {
                        if(student1 == "X")
                        {
                            correct = correct + 0;
                        }
                        else if(student1 != answer)
                        {
                            correct = correct - 1;
                        }
                        correct = correct + 1;
                    }
                    Console.WriteLine(a);
                }*/
                Console.WriteLine(students);
            }
            Console.ReadKey();
        }
    }
}




任何帮助都将在解决问题时受到赞赏!谢谢!

Any help would be appreciated in solving the problem! Thanks!

推荐答案

问候奥斯汀。在学生词典中添加条目时,您要将每个条目的值设置为该键。但是密钥长度只有4个字符,因此只要您尝试读取第5个字符,就会出现超出范围的异常。值
应该是学生答案的20个字符的字符串。

Greetings Austin. When you add an entry to the students dictionary, you are setting the value of each entry to the key. But the key is only 4 characters long, so as soon as you try to read the 5th character, you get an out of range exception. The value should be the 20 character string of the student's answers.

            while (!reader.EndOfStream)
            {
               currentLine = reader.ReadLine();
               idAnswerPair = currentLine.Split(' ');

               if (idAnswerPair.Length > 1) // You need this condition to prevent a crash on the last line ("0").
               {
                  students.Add(idAnswerPair[0], idAnswerPair[1]); // Key is [0], value is [1].
               }


            }


这篇关于帮助我的代码进行分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆