【发布时间】: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)。