我必须制作一个生成乐透彩票的程序,然后检查是否有赢家。我甚至在调用我的方法时遇到了问题,这只是一个简单的菜单功能。
这是错误:
错误1非静态字段需要对象引用, 方法或财产
我不确定我哪里出错但我认为这与可见性有关。
我已尝试将某些功能更改为公共静态,但这种功能无效。
class Program
{
bool quitFlag = false;
SortedSet<int> winningNumbers = new SortedSet<int>();
List<CTicket> ticketList = new List<CTicket>();
static void Main(string[] args)
{
DoMenu();
}
public static void GenerateTickets(List<CTicket> l)
{
Random Rand = new Random();
for(int i=0; i <1000; i++)
{
SortedSet<int> num = new SortedSet<int>();
do
{
num.Add(Convert.ToInt32(Rand.Next(1,42)));
}
while(num.Count <=6);
CTicket ticket = new CTicket(i,num);
l.Add(ticket);
ticket.printTicket();
}
}
public static void DrawWinningNumbers(SortedSet<int> winningNumbers)
{
for(int i=1; i <6; i++)
{
Random Rand = new Random();
winningNumbers.Add(Convert.ToInt32(Rand.Next(1,42)));
while(winningNumbers.Count <=6);
Console.Write("The winning numbers are: {");
foreach (int x in winningNumbers)
{
Console.Write(x + ", ");
}
Console.WriteLine();
}
}
void DoQuit()
{
quitFlag = true;
}
int ShowMenu()
{
int option;
Console.WriteLine("\t-------------------------------------------------\n");
Console.WriteLine( "\t\t\t Queue Application\n\n");
Console.WriteLine("\t1. \tGenerate tickets\n");
Console.WriteLine("\t2. \tDisplay all tickets\n");
Console.WriteLine("\t3. \tGenerate Winning Numbers\n");
Console.WriteLine("\t4. \tPrint queue\n");
Console.WriteLine("\t5. \tReverse queue\n");
Console.WriteLine("\t6. \tExit\n");
option = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine();
return option;
}
public static void DoMenu()
{
do
{
int option;
option = ShowMenu();
switch (option)
{
case 1:
GenerateTickets(ticketList);
//system("cls");
break;
case 2:
//system("cls");
break;
case 3:
DrawWinningNumbers(winningNumbers);
//system("cls");
break;
case 4:
break;
case 5:
//system("cls");
break;
case 6:
DoQuit();
break;
default:
//system("cls");
Console.WriteLine("invalid option!\n");
break;
}
} while (quitFlag != true);
}
}
答案 0 :(得分:0)
错误应该是明确的 - 您正在从静态方法访问非静态项(quitFlag
,winningNumbers
,ticketList
)。
将上面的项目更改为static
(如果它们应该由Program
类的所有实例共享),或者使方法非静态并在实例上调用< / {>的Program
。