简体   繁体   English

(CS0266、CS0120)我可以通过查看列表的实例来避免被要求访问我的列表吗?

[英](CS0266, CS0120) Can I get around being asked to access my list by going through an instance of it?

I'm having trouble compiling this program because of the errors noted in the title.由于标题中提到的错误,我无法编译此程序。 My aim is for this program to output the zeros of a quadratic function.我的目标是为这个程序 output 二次 function 的零点。

I tried using.ElementAt(at line 14) to rectify the issue but I am apparently attempting to implement it improperly.我尝试使用 .ElementAt(at line 14) 来纠正这个问题,但我显然试图不正确地实施它。 I don't understand why I can't access my List (line 25).我不明白为什么我不能访问我的列表(第 25 行)。

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
    int quad(List<double> terms) //takes input from terms list and outputs a list of doubles
    {
        double discriminant = Math.Sqrt(terms[1] * terms[1] - 4 * terms[0] * terms[2]);
        double quad1 = (-terms[1] + discriminant) / 2 * terms[0];
        double quad2 = (-terms[1] - discriminant) / 2 * terms[0];
        double[] quadOutput = new double[] { quad1, quad2 };
        foreach (var term in terms)
        {
            Console.WriteLine(quadOutput[term]); 
        }
        return 0;
    }



    static void Main(string[] args)
    {
        Console.WriteLine("Please enter terms (a,b,c)");
        var userInput = Console.ReadLine();
        List<double> terms = userInput.Split(',') //static?
                             .Select(Convert.ToDouble)
                             .ToList();
        quad(terms);
    }
}

I'm using mcs to compile on linux(arch64)我正在使用 mcs 在 linux(arch64) 上编译

Error Codes:错误代码:

CS0266: Cannot implicitly convert type double' to int'. CS0266:无法将类型double' to int'。 An explicit conversion exists (are you missing a cast?)存在显式转换(您是否缺少演员表?)

CS0120: An object reference is required to access non-static member `Program.quad(System.Collections.Generic.List)' CS0120:需要 object 引用才能访问非静态成员“Program.quad(System.Collections.Generic.List)”

Edit: Fixed both original problems but now i think the conversion from double to int (for the index) is rounding up somehow?编辑:修复了两个原始问题,但现在我认为从 double 到 int(对于索引)的转换正在以某种方式四舍五入? Program compiles but upon running(given input) it gives multiple index out of bounds errors.程序编译但在运行时(给定输入)它给出了多个索引越界错误。

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
    int quad(List<double> terms) //takes input from terms list and outputs a list of doubles
    {
        double discriminant = Math.Sqrt(terms[1] * terms[1] - 4 * terms[0] * terms[2]);
        double quad1 = (-terms[1] + discriminant) / 2 * terms[0];
        double quad2 = (-terms[1] - discriminant) / 2 * terms[0];
        double[] quadOutput = new double[] { quad1, quad2 };
        foreach (var term in terms)
        {
            Console.WriteLine(quadOutput[(Convert.ToInt32(term))]); //how is this converting double to int?
        }
        return 0;
    }



    static void Main(string[] args)
    {
        Console.WriteLine("Please enter terms (a,b,c)");
        var userInput = Console.ReadLine();
        List<double> terms = userInput.Split(',') 
                             .Select(Convert.ToDouble)
                             .ToList();
        Program q = new Program();
        q.quad(terms);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
    int quad(List<double> terms) //takes input from terms list and outputs a list of doubles
    {
        double discriminant = Math.Sqrt(terms[1] * terms[1] - 4 * terms[0] * terms[2]);
        double quad1 = (-terms[1] + discriminant) / 2 * terms[0];
        double quad2 = (-terms[1] - discriminant) / 2 * terms[0];
        double[] quadOutput = new double[] { quad1, quad2 };
        foreach (var output in quadOutput) //works as expected
        {
            Console.WriteLine(output);
        }
        return 0;
    }



    static void Main(string[] args)
    {
        Console.WriteLine("Please enter terms (a,b,c)");
        var userInput = Console.ReadLine();
        List<double> terms = userInput.Split(',') 
                             .Select(Convert.ToDouble)
                             .ToList();
        Program q = new Program();
        q.quad(terms);
    }
}

Sorry for the confusion, I was for some reason trying to use the terms List to index.对不起,我出于某种原因试图使用术语列表来索引。 I guess I'm tired.我想我累了。 Program now works as intended.程序现在按预期工作。

quadOutput is an array of doubles terms is a list of doubles quadOutput 是一个双精度数组术语是一个双精度列表

you're trying to say "give me quadOutput at index of (double)"你想说“在(双)索引处给我quadOutput”

that's why it's invalid.这就是为什么它是无效的。 you wouldn't be able to say quadOutput[1.3] because you can't get an index of 1.3 of an array你不能说 quadOutput[1.3] 因为你不能得到一个数组的 1.3 的索引

either turn your terms to a list of ints or cast your 'term' to an int要么将您的术语转换为整数列表,要么将您的“术语”转换为整数

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

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