【问题标题】:cannot declare instance members in a static class不能在静态类中声明实例成员
【发布时间】:2020-03-06 12:42:18
【问题描述】:

编辑 好的,所以我将课程设置为非静态的,这有帮助。但是现在我收到一个错误,即程序 main.exe 不包含适合入口点的静态“Main”方法。

C# 新手。请帮忙。我如何使这项工作?本课的目标是在 main 中调用一个函数。我只用 C++ 编程过。

所以我从课堂上删除了“静态”或厌倦了公开字符串。还是不行。

我意识到这不是一个非常有效的程序,但它主要是一种学习练习。

using System;

namespace survey{

static class program {

public string GetName()
{
  Console.WriteLine("\nPlease input name > ");
  string name = Console.ReadLine();

  if (name == "") 

  {
    do {
      Console.Write("Invalid input. Please try again > ");
      name = Console.ReadLine();
       } while ( name == "");
  }
  return name;
}

public string GetYear()
{
  Console.WriteLine("\nPlease input year > ");
  string name = Console.ReadLine();

  if (year == "") 
  {
    do {
      Console.Write("Invalid input. Please try again > ");
      year = Console.ReadLine();
       } while ( year == "");
  }
  return year;
}

public string GetAge()
{
  Console.WriteLine("\nPlease input age > ");
  string age = Console.ReadLine();

  if (age == "") 
  {
    do {
      Console.Write("Invalid input. Please try again > ");
      age = Console.ReadLine();
       } while ( age == "");
  }
  return age;
}

static void Main (string[] args){

Console.WriteLine("\nPlease note\nThis program is only applicable for users born between 1999 and 2010");

string name = GetName();
string year = GetYear();
string age = GetAge();

Console.WriteLine("\nYour name is: " + name);
Console.WriteLine("Your age is: " + age);

 if(year == "1999"){
  Console.WriteLine("You were born in the year of the rabbit");
}

else if(year == "2000"){
  Console.WriteLine("You were born in the year of the dragon");
}

else if(year == "2001"){
  Console.WriteLine("You were born in the year of the snake");
}
else if(year == "2002"){
  Console.WriteLine("You were born in the year of the horse");
}
else if(year == "2003"){
  Console.WriteLine("You were born in the year of the goat");
}

else if(year == "2004"){
  Console.WriteLine("You were born in the year of the monkey");
}

else if(year == "2005"){
  Console.WriteLine("You were born in the year of the rooster");
}

else if(year == "2006"){
  Console.WriteLine("You were born in the year of the dog");
}

else if(year == "2007"){
  Console.WriteLine("You were born in the year of the pig");
}

else if(year == "2008"){
  Console.WriteLine("You were born in the year of the dragon");
}

else if(year == "2009"){
  Console.WriteLine("You were born in the year of the ox");
}

else if(year == "2010"){
  Console.WriteLine("You were born in the year of the tiger");
}

else{
  Console.WriteLine("Invalid year");
}
}
}
}

【问题讨论】:

  • 你在哪一行得到这个错误?
  • @faithfull 好的,所以我将课程设置为非静态的,这有帮助。但是现在我收到一个错误,即程序 main.exe 不包含适合入口点的静态“Main”方法。
  • Main 是一个静态函数。它不能访问非静态数据/函数。要么创建函数static,要么创建一个program 对象(从类中删除static)。

标签: c# static


【解决方案1】:

我不确定我是否正确理解了这里的意图,但如果你想在不启动对象的情况下访问成员,这是要走的路:

namespace survey
{
class program
    {
        private static string year;

        public static string GetName()
        {
            Console.WriteLine("\nPlease input name > ");
            string name = Console.ReadLine();

            if (name == "")

            {
                do
                {
                    Console.Write("Invalid input. Please try again > ");
                    name = Console.ReadLine();
                } while (name == "");
            }
            return name;
        }

        public static string GetYear()
        {
            Console.WriteLine("\nPlease input year > ");
            string name = Console.ReadLine();

            if (year == "")
            {
                do
                {
                    Console.Write("Invalid input. Please try again > ");
                    year = Console.ReadLine();
                } while (year == "");
            }
            return year;
        }

        public static string GetAge()
        {
            Console.WriteLine("\nPlease input age > ");
            string age = Console.ReadLine();

            if (age == "")
            {
                do
                {
                    Console.Write("Invalid input. Please try again > ");
                    age = Console.ReadLine();
                } while (age == "");
            }
            return age;
        }

        static void Main(string[] args)
        {

            Console.WriteLine("\nPlease note\nThis program is only applicable for users born between 1999 and 2010");

            string name = GetName();
            string year = GetYear();
            string age = GetAge();

            Console.WriteLine("\nYour name is: " + name);
            Console.WriteLine("Your age is: " + age);

            if (year == "1999")
            {
                Console.WriteLine("You were born in the year of the rabbit");
            }

            else if (year == "2000")
            {
                Console.WriteLine("You were born in the year of the dragon");
            }

            else if (year == "2001")
            {
                Console.WriteLine("You were born in the year of the snake");
            }
            else if (year == "2002")
            {
                Console.WriteLine("You were born in the year of the horse");
            }
            else if (year == "2003")
            {
                Console.WriteLine("You were born in the year of the goat");
            }

            else if (year == "2004")
            {
                Console.WriteLine("You were born in the year of the monkey");
            }

            else if (year == "2005")
            {
                Console.WriteLine("You were born in the year of the rooster");
            }

            else if (year == "2006")
            {
                Console.WriteLine("You were born in the year of the dog");
            }

            else if (year == "2007")
            {
                Console.WriteLine("You were born in the year of the pig");
            }

            else if (year == "2008")
            {
                Console.WriteLine("You were born in the year of the dragon");
            }

            else if (year == "2009")
            {
                Console.WriteLine("You were born in the year of the ox");
            }

            else if (year == "2010")
            {
                Console.WriteLine("You were born in the year of the tiger");
            }

            else
            {
                Console.WriteLine("Invalid year");
            }
        }
    }
}

【讨论】:

  • 我试过了,但它说要等 4 分钟~我保持标签打开,所以我会确保我这样做。你帮了大忙!
  • 完美,很高兴听到这个消息:)
【解决方案2】:

也将函数设为静态,即

public static string GetAge() {...}

而不是

public string GetAge() {...}

【讨论】:

  • + 你在GetYear函数中有一个错误:变量名应该是'year'而不是'name'。
猜你喜欢
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 2012-05-04
相关资源
最近更新 更多