无法在静态类C#中声明实例成员

时间:2016-04-16 20:13:31

标签: c#

我收到错误

cannot declare instance members in a static class

尝试编译我的控制台应用程序时。我已经声明保存字符串和整数的方法是静态的,但我的代码不会编译。我也检查了这两个问题,但他们并没有真正帮助。

Error cannot declare instance members in a static class

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

public static class Alg
{


    string file = @"CMP1124M_Weather_Data\Year.txt";
    string readin = File.ReadAllText(file);    // Reads contents of text file
    int[] Output = LoadIntegersFromFile(file);

    static int[] LoadIntegersFromFile(string file) // Converts txt files to int array
    {
        return File.ReadLines(file).Select(int.Parse).ToArray();
    }

    public static void Main()
    {
        string[] WS1AF = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS1_AF.txt");
        string[] WS1Rain = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS1_Rain.txt");
        string[] WS1Sun = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS1_Sun.txt");
        string[] WS1TMax = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS1_TMax.txt");
        string[] WS1TMin = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS1_TMin.txt");
        string[] WS2AF = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS2_AF.txt");
        string[] WS2Rain = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS2_Rain.txt");
        string[] WS2Sun = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS2_Sun.txt");
        string[] WS2TMax = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS2_TMax.txt");
        string[] WS2TMin = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\WS2_TMin.txt");
        string[] Month = System.IO.File.ReadAllLines(@"CMP1124M\Weather_Data\Month.txt");

        int key; // Year the user wants to find i.e 1955
        int low = 0;
        int high = 1021; // These two are the size of the arrays
        string outputtext = " "; // This displays text to user.

    }


    public static int SearchMethod1()
    {
        int BS = SearchMethod.BinarySearch_R(key, Output, low, high); // Calls the Binary Search Method from 'SearchMethod' Class
        int BSLB = SearchMethod.lower_bound(key, Output, low, high);    // scans left & finds last element == key
        int BSUB = SearchMethod.upper_bound(key, Output, low, high); // scans right & finds first element not == key
        int UB = BSUB - 1;
        Console.WriteLine(" BinarySearch Completed:" + BS);
        SearchMethod.BinarySearchDisplay(UB, BSLB, outputtext, Year, Month, WS1AF, WS1Rain, WS1Sun, WS1TMax, WS1TMin);
    }


    public static int BinarySearch_R(int key, int[] array, int low, int high)
    {
        if (low > high) return -1;
        int mid = (low + high) / 2;
        if (key == array[mid])
        {

            return mid;
        }
        if (key < array[mid])
        {
            return BinarySearch_R(key, array, low, mid - 1);
        }
        else
        {
            return BinarySearch_R(key, array, mid + 1, high);
        }
    }


    public static int lower_bound(int key, int[] arr, int low, int high)
    {
        if (low > high)
            //return -1;
            return low;

        int mid = low + ((high - low) >> 1);
        //if (arr[mid] == key) return mid;


        if (arr[mid] >= key)
            return lower_bound(key, arr, low, mid - 1);
        else
            return lower_bound(key, arr, mid + 1, high);
    }


    //Upper Bound
    public static int upper_bound(int key, int[] arr, int low, int high)
    {
        if (low > high)
            //return -1;
            return low;

        int mid = low + ((high - low) >> 1);
        //if (arr[mid] == key) return mid;

        //Attention here, we go right for upper_bound when meeting equal values
        if (arr[mid] > key)
            return upper_bound(key, arr, low, mid - 1);
        else
            return upper_bound(key, arr, mid + 1, high);
    }
    ///////////////////////////////

    //Binary search display
    public static void BinarySearchDisplay(int UB, int BSLB, string outputtext, string[] Year, string[] Month, string[] WS1AF, string[] WS1Rain, string[] WS1Sun, string[] WS1TMax, string[] WS1TMin)
    {
        Console.WriteLine(" Upper Bound: " + UB);
        Console.WriteLine(" Lower Bound: " + BSLB);
        Console.ReadLine();

        Console.WriteLine("\t\tYear\tMonth\t\tAF\tRain\tSun\tTMax\tTMin");
        do
        {
            int PrintKey = UB;

            outputtext += "\t" + Year[PrintKey];
            outputtext += "\t" + Month[PrintKey];
            outputtext += "\t\t" + WS1AF[PrintKey];
            outputtext += "\t" + WS1Rain[PrintKey];
            outputtext += "\t" + WS1Sun[PrintKey];
            outputtext += "\t" + WS1TMax[PrintKey];
            outputtext += "\t" + WS1TMin[PrintKey];
            Console.WriteLine(" Your choice: {0}", outputtext);
            Console.WriteLine("");
            UB--;
            outputtext = " ";

        }

        while (UB >= BSLB);

        Console.ReadLine();
    }









} 

我可能刚刚跳过一些简单的东西,所以任何帮助都会受到赞赏。

0 个答案:

没有答案