C#如何通过用户输入用浮点值模块化地填充列表,计算总和和平均值
本文关键字:列表 填充 计算 平均值 模块化 用户 何通过 输入 | 更新日期: 2023-09-27 18:20:10
请注意,我有C++背景,我的导师应该被称为MOST的作业监督者。没有任何指示或指导。现在,有没有可能有好心人能帮助我,帮我找出我做错了什么,然后解释(用杜尔杜尔语言)如何修复它,使其编译???这是代码和错误。从22点开始,但最后9点要了我的命,现在是凌晨3点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace P1_Wilson
{
class P1_Wilson
{
float sum;
float aver;
int counter;
static float FillNumber()//Declaration of method utilized to fill an array/list of floating point numbers.
{
int counter = 0;
List<float> floatlist = new List<float>();
char sentinel;
Console.WriteLine("I would like you to now input a few floating point numbers.'n Enter q to quit.");
Console.WriteLine("You have the option to quit now.'nWould you like to continue? 'nEnter q to quit.");
sentinel = char.Parse(Console.ReadLine());
while (sentinel != 'q')
{
Console.WriteLine("You have the option to quit now.'nWould you like to continue?'nEnter q to quit.");
sentinel = char.Parse(Console.ReadLine());
Console.WriteLine("Then please input a floating point number.");
floatlist[counter] = float.Parse(Console.ReadLine(floatlist[counter]));
counter++;
return floatlist;
return counter;
}
}
static float CalcSum(List<float> floatlist, int counter)//Declaration of method used to add all entered numbers together and calculate the total sum.
{
for (; counter >= 0; counter--)
{
float sum = sum + floatlist[counter];
}
return sum;
}
static float CalcAver(float sum, float aver, int counter)//Declaration of method used to calculate the average of numbers entered.
{
aver = sum / counter;
return aver;
}
static void Main()//Where execution of program begins. Introduces user to program. Prompts for user input and calls all subservient methods.
{
FillNumber();
CalcSum();
CalcAver();
Console.WriteLine("The total number of floating point numbers entered is:" + counter);
Console.WriteLine("The total sum is:" + sum);
Console.WriteLine("The average of the numbers entered is:" + aver);
Console.ReadLine();
}
}
}
这些是我的错误:9
错误1方法"ReadLine"的无重载需要1个参数错误5方法"CalcSum"的重载不包含0个参数错误6方法"CalcAver"的重载不包含0个参数错误2无法将类型"System.Collections.Generic.List"隐式转换为"float"错误3非静态字段、方法或属性"P1_Wilson.P_Wilson.sum"需要对象引用错误8非静态字段、方法或属性"P1_Wilson.P_Wilson.sum"需要对象引用错误7非静态字段、方法或属性"P1_Wilson.P_Wilson.counter"需要对象引用错误9非静态字段、方法或属性"P1_Wilson.P_Wilson.aver"需要对象引用错误4"sum"与声明"P1_Wilson.P1_ilson.sum"冲突
如果我的一次拙劣的幽默尝试更多地引起了愤怒,而不是善意地引起笑声,我深表歉意。
逐一阅读错误消息并修复它们,例如:
-
错误1方法"ReadLine"没有重载需要1个参数
此错误消息并不含糊,特别是当Visual Studio应将您指向代码中发生错误的确切位置时。
在这种情况下,它是:
Console.ReadLine(floatlist[counter]);
请仔细阅读错误消息(如果需要,请在谷歌上搜索),逐一修复您的错误,如果您遇到特定错误,或者您希望在代码运行时进行代码审查,请返回。