【发布时间】:2011-12-17 16:59:04
【问题描述】:
我正在使用数组和方法。我正在编写一个代码来计算用户在数组中输入的数字的平均值。我想出了第一种方法,但 VS 告诉我“并非所有代码路径都返回一个值”。当我在 Main() 方法中测试代码时,它运行良好,但是当它在我的 GetValues() 方法中时,我得到了错误。
我阅读了所有其他帖子,但由于它们的特殊性,它们对我来说不太有意义。我知道这并不太难,但我是一个初学者,并试图自己理解这一点。
我的程序还没有完成,下面的代码只是我程序的第一部分(方法)。一旦 GetValues () 工作,想法就是从另一个计算平均值的方法调用这个方法。同样,GetValues() 应该捕获数组。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testscores
{
class Program
{
static void Main(string[] args)
{
}
private static int[] GetValues()
{
string inValue;
int[] score = new int[5];
int total = 0;
for (int i = 0; i < score.Length; i++)
{
Console.Write("Enter Score {0}: ", i + 1);
inValue = Console.ReadLine();
score[i] = Convert.ToInt32(inValue);
}
for (int i = 0; i < score.Length; i++)
{
total += score[i];
}
}
}
}
【问题讨论】: