简体   繁体   English

超出范围但在范围内?

[英]Out Of Range but it is in range?

So I want to learn the periodic table but I want to program a game to do it (It will be a console window). 因此,我想学习元素周期表,但想对游戏进行编程(它将是一个控制台窗口)。 So the first code is the class for an element, its pretty easy... 所以第一个代码是元素的类,这很容易...

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

namespace PeriodicTableGame
{
    class Element
    {
        int AtomicNumber;
        double AtomicMass;
        string Name;
        int Period;
        int Group;
        bool isLanthanide;
        bool isActanide;
        int isMetal; // 0 = Non Metal, 1 = Metaloid, 2 = Metal
        int stateAtRT; // 0 = Solid, 1 = Liquid, 2 = Gas, 3 = Plasma (68     Degrees F, 20 Celcius, 293.15 Kelvin)

        int Block; // 0 = s, 1 = d, 2 = p, 3 = f

        public Element(int atomicNumber, double atomicMass, int period, int group, bool islanthanide, bool isactanide, int ismetal, int stateatrt, int block)
        {
            AtomicNumber = atomicNumber;
            AtomicMass = atomicMass;
            Period = period;
            Group = group;
            isLanthanide = islanthanide;
            isActanide = isactanide;
            isMetal = ismetal;
            stateAtRT = stateatrt;
            Block = block;
        }
    }

So that block is good so far, and now the second block... 到目前为止,该区块很好,现在是第二个区块...

class Data
{
    public static List<Element> ElementList = new List<Element>();

    public static List<Element> AddToList()
    {
        Element Nothing = new Element(0, 0, 0, 0, false, false, 0, 0, 0);
        Element Hydrogen = new Element(1, 1.008, 1, 1, false, false, 0, 2, 0);
        Element Helium = new Element(2, 4.003, 1, 18, false, false, 0, 2, 0);
        Element Lithium = new Element(3, 6.94, 2, 1, false, false, 0, 0, 0);
        Element Beryllium = new Element(4, 9.012, 2, 2, false, false, 2, 0, 0);
        Element Boron = new Element(5, 10.81, 2, 13, false, false, 1, 0, 2);
        Element Test = new Element(0, 0, 0, 0, false, false, 0, 0, 0);
        ElementList.Insert(0, Nothing);
        ElementList.Insert(1, Hydrogen);
        ElementList.Insert(2, Helium);
        ElementList.Insert(3, Lithium);
        ElementList.Insert(4, Beryllium);
        ElementList.Insert(5, Boron);
        ElementList.Insert(6, Test);
        return ElementList;
    }
}

That one is also good, and now the 3rd block... 那一个也不错,现在是第三个街区...

class NumberGenerator
{
    public static int RandomNumberGenerator(int Min, int Max)
    {
        Random RandomGen = new Random();
        int RandomGenChoice = RandomGen.Next(Min, Max + 1);
        return RandomGenChoice;
    }
    public static Element RandomElementGenerator(int Number)
    {
        Element x = Data.ElementList.ElementAt(Number);
        return x;
    }
}

    class Program
{
    static void Main(string[] args)
    {
        int RNG = NumberGenerator.RandomNumberGenerator(1, 5);
        Element y = NumberGenerator.RandomElementGenerator(RNG);
    }

(Ignore the brackets I may have missed one on this post but they are fine in Visual Studio) I made a list of elements (I started with 5, and a 1st, and a test...) and tries to use the ElementGenerator on them but it gave an out of range exception, so I used an array, which is what you are looking at, and than I use the number 1 instead of 'Number' which is an Index no question, I put it in by hand, and I still got an out of range exception... WHY? (忽略了我可能在本文中遗漏了一个括号,但是在Visual Studio中它们没问题)我列出了一个元素列表(我从5开始,从1开始,还有一个测试...),并尝试在上使用ElementGenerator它们,但是它给出了超出范围的异常,所以我使用了一个数组,这就是您要查看的内容,然后我使用数字1代替了“数字”(这毫无疑问是索引),我将它放在了手而且我仍然有超出范围的异常...为什么?

I would recommend you post this code to Code Review , because there are several things you could do more easily using C# features. 我建议您将此代码发布到Code Review上 ,因为使用C#功能可以更轻松地完成几件事。

But going with the problem as it is... 但是要解决这个问题...

Element y = NumberGenerator.RandomElementGenerator(i);

Are you sure this line is typed here as it is in your code? 您确定在此输入的行与您在代码中输入的一样吗? Because I could understand the error if it were really this: 因为如果确实是这样,我可以理解该错误:

Element y = NumberGenerator.RandomElementGenerator(j);  // j, not i

Here is what your Main method is doing: 这是您的Main方法正在做的事情:

static void Main(string[] args)
{
    // generate random number from 1 to 5
    int i = NumberGenerator.RandomNumberGenerator(5, 1);

    // print the number
    Console.WriteLine(i);

    // set j equal to (1 to 5) - 7 + 1 = (-6 to -1) [but this line is unused]
    int j = i - Data.ElementList.Count + 1;

    // Get an element in position -6 to -1
    Element y = NumberGenerator.RandomElementGenerator(j);

    // Wait for user to press a key
    Console.ReadKey();
}

If that fourth line is j as I believe, then you're trying to get the element at a position from -1 to -6, but an array will only have 0 and positive number positions. 如果我相信那条第四行是j ,那么您试图将元素放在-1到-6的位置,但是数组将只有0和正数位置。

You can use your debugger to find out what Number contains. 您可以使用调试器找出Number包含的内容。 When you get the exception, hover over Number to see what value it has. 收到异常后,将鼠标悬停在Number即可查看其值。 In Visual Studio 2017, it might show you this value automatically anyhow. 在Visual Studio 2017中,无论如何它可能会自动向您显示此值。

You are calling an empty list. 您正在呼叫一个空列表。 Add items your Data.ElementList via Data.AddToList() first before calling the index. 通过Data.AddToList()首先调用索引之前添加的项目您Data.ElementList。

    public static Element RandomElementGenerator(int Number)
    {
        Data.AddToList();
        Element x = Data.ElementList.ElementAt(Number);
        return x;
    }

Then you can call the index in the Data.ElementList . 然后,您可以在Data.ElementList中调用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM