Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多维数组索引超出范围。C#_C#_Arrays_Multidimensional Array - Fatal编程技术网

多维数组索引超出范围。C#

多维数组索引超出范围。C#,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我有一个二维阵列,它是5跨8。当我不输入值时,它会工作。但只要我输入一个值,它就有超出范围的错误 这是测验的一部分 int score = 0; string question; Console.WriteLine("You will be asked a random question from one of the topics at a random difficulty"); for (int i = 0; i <= 9; i++) { Random

我有一个二维阵列,它是5跨8。当我不输入值时,它会工作。但只要我输入一个值,它就有超出范围的错误

这是测验的一部分

int score = 0;
string question;
Console.WriteLine("You will be asked a random question from one of the topics at a random difficulty");

for (int i = 0; i <= 9; i++)
{
    Random random = new Random();
    int x = random.Next(2);
    int y = random.Next(7);
    Console.WriteLine(Questions[x, y]);
    question = Console.ReadLine().ToUpper();
    if (question == (Answers[x, y]))
    {
        Console.WriteLine("Correct");
        score++;
    }
}
static string[,] Answers =
{
    { "A", "B", "B", "A", "A" },
    { "A", "A", "B", "A", "A" },
    { "A", "B", "A", "B", "A" }
};

static string[,] Questions =
{
    { "1.Name the organelles where protiens are synthesised? A.Ribosome or B.Mitchodria: ",
        "2.What enzyme breaks down carbohydrases? A.Lipids or B.Carbohydrases: ",
        "3.What is meant by the definition of comunicable disease? A.The disease can talk or B.A disease that can be transmitted from person to person: ",
        "4.Define photosynthesis? A.the process where plants sunlight to synthesize nutrients from carbon dioxide and water or B.Where plants take pictures: ",
        "5.What part of your body never stops growing? A.Ears or B.Hands: " },
    { "1.Name the organelles where protiens are synthesised? A.Ribosome or B.Mitchodria or C.Nucleaus: ",
        "2.What enzyme breaks down carbohydrases? A.Lipids or B.Carbohydrases or C.Protease: ",
        "3.What is meant by the definition of comunicable disease? A.The disease can talk or B.A disease that can be transmitted from person to person or C.A disease that can\"t be transmitted from person to person: ",
        "4.Define photosynthesis? A.the process where plants sunlight to synthesize nutrients from carbon dioxide and water or B.Where plants take pictures or C.Where plants use sunlight and oxygen to synthesize nutrients?: ",
        "5.What part of your body never stops growing? A.Ears or B.Hands or C.Feet: " },
    { "1.Name the organelles where protiens are synthesised? A.Ribosome or B.Mitchodria or C.Nucleaus or D.Vacuole: ",
        "2.What enzyme breaks down carbohydrases? A.Lipids or B.Carbohydrases or C.Protease or D.Amylase: ",
        "3.What is meant by the definition of comunicable disease? A.The disease can talk or B.A disease that can be transmitted from person to person or C.A disease that can\"t be transmitted from person to person or D.A communist disease: ",
        "4.Define photosynthesis? A.the process where plants sunlight to synthesize nutrients from carbon dioxide and water or B.Where plants take pictures or C.Where plants use sunlight and oxygen to synthesize nutrients or D.Where plants user CO2 and glucose to produce Oxygen: ",
        "5.What part of your body never stops growing? A.Ears or B.Hands or C.Feet D.Body: "},
    { "1.What is largest planet in our solar system? A.Jupiter or B.Saturn: ",
        "2.Which of the following planets does not have rings? A.Mars or B.Saturn: ",
        "3.How long does it take the Earth to complete a full orbit around the Sun? A.2 years or B.1 year: ",
        "4.Whats the biggest star in our solar system? A.The sun or B.The moon: ",
        "5.What is the hottest planet in our solar system? A.Mercury or B.Venus: "},
    { "1.What is largest planet in our solar system? A.Jupiter or B.Saturn or C.Neptune: ",
        "2.Which of the following planets does not have rings? A.Mars or B.Saturn or C.Juipter: ",
        "3.How long does it take the Earth to complete a full orbit around the Sun? A.2 years or B.1 year or C.10 years: ",
        "4.Whats the biggest star in our solar system? A.The sun or B.The moon or C.The Earth: ",
        "5.What is the hottest planet in our solar system? A.Mercury or B.Venus or C.Mars: "},
    { "1.What is largest planet in our solar system? A.Jupiter or B.Saturn or C.Neptune or D.Uranus: ",
        "2.Which of the following planets does not have rings? A.Mars or B.Saturn or C.Jupiter or D.Uranus: ",
        "3.How long does it take the Earth to complete a full orbit around the Sun? A.2 years or B.1 year or C.10 years or D.100 years: ",
        "4.Whats the biggest star in our solar system? A.The sun or B.The moon or C.The Earth or D.None of these: ",
        "5.What is the hottest planet in our solar system? A.Mercury or B.Venus or C.Mars or D.Pluto: "},
    { "1.Hexadecimal means? A.Base 16 or B.Base 8: ",
        "2.How many bits are in a byte? A.6 or B.8: ",
        "3.How many MB in a Gigga byte A.1024MB or B.2028MB: ",
        "4.Define open source software A.It\"s an open bottle of ketchup or B.It\"s a free program: ",
        "5.Which of these is an example of secondry? A.SSD or B.Cache: "},
    { "1.Hexadecimal means? A.Base 16 or B.Base 8 or C.base 10: ",
        "2.How many bits are in a byte? A.6 or B.8 or C.16: ",
        "3.How many MB in a Gigga byte A.1024MB or B.2028MB or C.1024kb: ",
        "4.Define open source software A.It\"s an open bottle of ketchup or B.It\"s a free program OR C.It\"s a paid subscription: ",
        "5.Which of these is an example of secondry? A.SSD or B.Cache or C.RAM: "},
    { "1.Hexadecimal means? A.Base 16 or B.Base 8 or C.base 10 or D.Base 2: ",
        "2.How many bits are in a byte? A.6 or B.8 or C.16 or D.4: ",
        "3.How many MB in a Gigga byte A.1024MB or B.2028MB or C.1024kb or D.3072: ",
        "4.Define open source software A.It\"s an open bottle of ketchup or B.It\"s a free program OR C.It\"s a paid subscription or D.None of these: ",
        "5.Which of these is an example of secondry? A.SSD or B.Cache or C.RAM or D.ROM: "}
};
int评分=0;
字符串问题;
WriteLine(“你将被问到一个随机的问题,从一个主题在一个随机的困难”);

因为(inti=0;i这两条线让你感到悲伤:

int x = random.Next(2);
int y = random.Next(7);
x
的值只能是
0
1
y
的值可以是
0
6

由于数组在第二维度中只有
5
个元素,因此当
y==5
y==6
时,就会出现错误


这里有一些东西供你玩:

    var question = Questions[random.Next(Questions.Length)];
    
    var options =
        question
            .Answers
            .OrderBy(x => random.Next())
            .Select((a, n) => new { a, o = (char)(n + 'A') })
            .ToDictionary(x => x.o, x => x.a);

    Console.WriteLine(question.Question);
    foreach (var option in options.OrderBy(x => x.Key))
    {
        Console.WriteLine($"{option.Key}: {option.Value}");
    }

    var answer = Console.ReadLine().ToUpper().First();
    
    if (options[answer] == question.Answers[0])
    {
        Console.WriteLine("Correct");
        score++;
    }


static (string Question, string[] Answers)[] Questions = new (string Question, string[] Answers)[]
{
    ("Name the organelles where protiens are synthesised?", new [] { "Lipids", "Carbohydrases" }),
    ("What enzyme breaks down carbohydrases?", new [] { "Carbohydrases", "Lipids" }),
    ("What is meant by the definition of comunicable disease?", new [] { "A disease that can be transmitted from person to person.", "The disease can talk." }),
    ("Define photosynthesis?", new [] { "the process where plants sunlight to synthesize nutrients from carbon dioxide and water.", "Where plants take pictures." }),
    ("What part of your body never stops growing?", new [] { "Ears", "Hands" }),
};

问题和答案放在一起,第一个答案总是正确的(当被问到时,它们会被混淆).

答案的维度似乎与问题不匹配…问题是5跨9下,但答案是5跨3下?你的答案数组是3*5,但你随机3*8->超出范围。斯威普先生也是对的!@MarioWu-OP的随机值是
2*7
,而不是
3*8
。@Engmativity啊,对,无法达到最大值,我记不起来了,抱歉!