你好,我们目前正在做一个数组任务,但我是c#的新手
本文关键字:任务 数组 新手 一个 目前 我们 你好 | 更新日期: 2023-09-27 18:08:03
我们必须找到用户输入的5个数字的平均值和或总和,但我不确定如何做到这一点。任何帮助都是感激的!这是我到目前为止的代码:基本上,我需要帮助完成输入的数字的总和,以及如何使用稍后在代码中输入的数字的数组,考虑到它声明'int unassigned'。
{
class Program
{
static void Main(string[] args)
{
int numofnum;
int[] numbers = new int[numofnum];
int index;
string avsu;
int average;
index = 0;
Console.WriteLine("Please enter the number of numbers you would like to find the average for, or add together: ");
numofnum = int.Parse(Console.ReadLine());
Console.WriteLine("Would you like to find the average or sum of these numbers? (average/sum)");
avsu = Console.ReadLine();
if (avsu == "average")
{
do
{
Console.Write("Please enter Number to find the average of: " + index);
numbers[index] = int.Parse(Console.ReadLine());
index = index + 1;
} while (index < numofnum);
index = 0;
do
{
Console.WriteLine("You entered the following values: ");
Console.WriteLine("Number " + index + ": " + numbers[index]);
index = index + 1;
Console.WriteLine("The average of these numbers is: " + (numbers[index] / 2));
} while (index < numofnum);
else if (avsu == "sum")
{
do
{
Console.Write("Please enter Number " + index + " to add: ");
numbers[index] = int.Parse(Console.ReadLine());
index = index + 1;
} while (index < numofnum);
index = 0;
do
{
Console.WriteLine("You entered the following values: ");
Console.WriteLine("Number " + index + ": " + numbers[index]);
index = index + 1;
Console.WriteLine("The sim of these numbers is: " + (numbers[index])); // UNSURE HOW TO DISPLAY THE SUM!!!!
} while (index < numofnum);
Console.ReadLine();
}
}
}
}
这有帮助吗?
static void Main(string[] args)
{
Console.WriteLine(
"Please enter the number of numbers you would like to find the average for, or add together: ");
int numofnum = int.Parse(Console.ReadLine());
int[] numbers = new int[numofnum];
Console.WriteLine("Would you like to find the average or sum of these numbers? (average/sum)");
var avsu = Console.ReadLine();
var sum = 0;
if (avsu != "average" && avsu != "sum")
{
return;
}
for (var i = 0; i < numofnum; i++)
{
Console.Write("Please enter Number " + i + " to add: ");
numbers[i] = int.Parse(Console.ReadLine());
}
for (var i = 0; i < numofnum; i++)
{
Console.WriteLine("You entered the following values: ");
Console.WriteLine("Number " + i + ": " + numbers[i]);
}
if (avsu == "sum")
{
sum = numbers.Sum();
Console.WriteLine("The sim of these numbers is: " + sum);
}
if (avsu == "average")
{
var average = sum / numbers.Length;
Console.WriteLine("The average of these numbers is: " + average);
}
}