“并非所有代码路径都返回值". [英] "Not all code paths return a value"

查看:80
本文介绍了“并非所有代码路径都返回值".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理数组和方法.我正在编写代码,该代码将计算用户在数组中输入的数字的平均值.我想出了第一种方法,但是VS告诉我并非所有代码路径都返回一个值".当我在Main()方法中测试代码时,它可以很好地工作,但是当它在我的GetValues()方法中时,我得到了错误.

I'm working with arrays and methods. I'm working on writing a code that will calculate the average of numbers entered by user in an array. I came up with the first method but VS tells me that "not all code paths return a value". When I tested the code in the Main() method it works well but when it's inside my GetValues() method i get the error.

我阅读了所有其他文章,但是由于它们的特殊性,它们对我而言不太有意义.我知道这不太困难,但是我是一个初学者,想自己理解这一点.

I read all other posts but they not quite make sense to me because of their specificity. I know this isn't too difficult but I'm a beginner and trying to understand this myself.

我的程序尚未完成,以下代码只是我程序的第一部分(方法).一旦GetValues()工作,便是从另一个将计算平均值的方法中调用此方法.同样,GetValues()应该捕获数组.

My program isn't done yet, the following code is just the first part (method) of my program. Once GetValues () work, the idea is then to call this method from another method that will calculate the average. Again, GetValues() is supposed to capture the array.

代码如下:

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];
            }
        }
    }
}

推荐答案

您是否要返回整数数组score?

Are you trying to return the integer array score?

   ....
        for (int i = 0; i < score.Length; i++)
        {
            total += score[i];
        }
        return score;
    }

因此,您可以像这样调用它来捕获该数组

So you can capture this array when you call it like so

int[] scores = GetValues();

这篇关于“并非所有代码路径都返回值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆