在 MicroSoft Visual Studio 2010 中使用 C# 作为控制台程序,在你们的帮助下,我对此进行了一些更改,并且该控制台程序运行正常;但是我需要实现一个常量静态字段,该字段将在 main 方法的输出部分显示座右铭“遵守女童子军法”。我知道这一定很简单,所以请多多包涵。当我在低音类中包含 public static const string Motto = "To Obey the Girl Scout Law" 时,我收到一条错误消息 - 常量 'DemoScouts.GirlScout.Motto' 不能是静态的。以下是该项目的完整代码:
公共课女童子军{
public static const string Motto = "To Obey the Girl Scout Law";
public static string scoutName;
public static string enterName()
{
return scoutName;
}
public static int duesOwed;
public static int enterAmount()
{
return duesOwed;
}
public static int troopNumber;
public static int enterNumber()
{
return troopNumber;
}
}
class MainClass : GirlScout
{
static void Main()
{
Console.WriteLine();
Console.Write("Enter the Girl Scout's name: ");
GirlScout.scoutName = Console.ReadLine();
Console.WriteLine();
Console.Write("Enter their Troop Number: ");
string n = Console.ReadLine();
GirlScout.troopNumber = Int32.Parse(n);
GirlScout.enterNumber();
Console.WriteLine();
Console.Write("Enter the amount they Owe in Dues: $");
string d = Console.ReadLine();
GirlScout.duesOwed = Int32.Parse(d);
GirlScout.enterAmount();
Console.WriteLine();
// Seperate the input from the output:
Console.WriteLine();
Console.WriteLine(GirlScout.Motto);
Console.WriteLine("-----------------------------------------------");
Console.WriteLine();
// Display the new information:
Console.WriteLine("The name of the Girl Scout is: {0}", GirlScout.scoutName);
Console.WriteLine("The troop Number of the Girl Scout is: {0}", GirlScout.troopNumber);
Console.WriteLine("The amount of Dues Owed by this Girl Scout is: {0}", GirlScout.duesOwed);
// Keep the console window open in debug mode.
Console.ReadKey();
}
}
}
任何和所有的建议将不胜感激。