我正在处理数组和方法。我正在编写一个代码,用于计算用户在数组中输入的数字的平均值。我想出了第一种方法,但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];
}
}
}
}
谢谢你,这正是我所做的。现在,如果我想从另一个方法调用返回的值。我应该向方法头添加一个参数吗?例如:private static int [] GetValues(int score) – okr
@Oskar不,这意味着你的意思是将一个int传入你的方法,你可以通过名字得分来访问......你不需要传递任何东西在,你正在创建一个名为score – heisenberg
@kekekela的本地数组谢谢!因此,如果我想从另一个方法调用存储在GetValues()中的值,我应该只是称它为从该方法键入“GetValues”?希望我在解释我自己 – okr