所以我试图通过Console从用户那里获得10个输入,将它们存储在一个数组中,在一个方法中计算总和是多少并返回它。我仍然在这个过程中,所以这并不意味着要完成代码,但我很困惑,为什么我的错误列表说Main应该在while语句后关闭?
class Program
{//Start class
//We need to declare the size of our array
const int ARRAYSIZE = 10;
static void Main()
{//Start main
//We need to create a counter for how many entries we currently have
int entries = 0;
int sumScore = 0;
//We need to create the array
int[] myArray = new int[ARRAYSIZE];
//Start a loop to ask for input
do
{//Start loop
Console.Write("Please enter the score of each test: ");
int entry = int.Parse(Console.ReadLine());
myArray[entries] = entry;
entries++;
}
while (entries < ARRAYSIZE);//End loop
static void PrintArray(int[ ] myArray)
{
foreach(int value in myArray)
{
Console.WriteLine(value);
Console.ReadLine();
}
}
}//End main
}//End class
答案 0 :(得分:2)
在static void PrintArray(int[ ] myArray)
的位置,您正在声明一个新功能。在声明新函数之前,您需要}//End main
:
do
{//Start loop
Console.Write("Please enter the score of each test: ");
int entry = int.Parse(Console.ReadLine());
myArray[entries] = entry;
entries++;
}
while (entries < ARRAYSIZE);//End loop
}//End main
static void PrintArray(int[ ] myArray)
{
foreach(int value in myArray)
{
Console.WriteLine(value);
Console.ReadLine();
}
}
答案 1 :(得分:1)
您错放了Main方法的结束括号。您可能还想在while (entries < ARRAYSIZE);//End loop
之后调用PrintArray方法,并在该方法中计算您的总和。但我想这是因为,正如你所说,这是一项正在进行中的工作。这是它的样子
class Program
{
const int ARRAYSIZE = 10;
static void Main()
{//Start main
//We need to create a counter for how many entries we currently have
int entries = 0;
int sumScore = 0;
//We need to create the array
int[] myArray = new int[ARRAYSIZE];
//Start a loop to ask for input
do
{//Start loop
Console.Write("Please enter the score of each test: ");
int entry = int.Parse(Console.ReadLine());
myArray[entries] = entry;
entries++;
}
while (entries < ARRAYSIZE);//End loop
PrintArray(myArray);
}//End main
static void PrintArray(int[ ] myArray)
{
int sum = 0;
foreach(int value in myArray)
{
sum += value;
Console.WriteLine(value);
Console.ReadLine();
}
Console.WriteLine(sum);
}
}
答案 2 :(得分:0)
这不是您问题的直接答案,但我认为这对您有用。
如果您想以稍微简单的方式执行此操作,请尝试以下操作:
static void Main()
{
var sum =
Enumerable
.Range(0, ARRAYSIZE)
.Select(n =>
{
Console.WriteLine("Please enter the score of each test:");
return int.Parse(Console.ReadLine());
})
.Sum();
Console.WriteLine();
Console.WriteLine("The sum is:");
Console.WriteLine(sum);
}