Program1.cs是一个普通的C#文件,运行良好。
Random numberGen = new Random();
int roll1 = 1;
int roll2 = 0;
int roll3 = 0;
int roll4 = 0;
int attempts = 0;
Console.WriteLine("Press enter to roll the dies");
while (roll1 != roll2 || roll2 != roll3 || roll3 != roll4 || roll4 != roll1)
{
Console.ReadKey();
roll1 = numberGen.Next(1, 7);
roll2 = numberGen.Next(1, 7);
roll3 = numberGen.Next(1, 7);
roll4 = numberGen.Next(1, 7);
Console.WriteLine("Dice 1: " + roll1 + "\nDice 2: " + roll2 + "\nDice 3: " + roll3 + "\nDice 4: " + roll4 + "\n");
attempts++;
}
Console.WriteLine("It took " + attempts + " attempts to roll a four of a kind.");
Console.ReadKey();
Program2.cs
Console.ReadKey();
在模块控制台下,它弹出一个错误: 只有一个编译单元可以拥有顶级语句。 错误:CS8802
我在终端中尝试了dotnet new console --force,但它最终只是删除了我的程序。 我想在同一个文件夹中运行多个C#文件,而不会出现只有一个编译单元可以拥有顶级语句或其他类似的错误。