English 中文(简体)
“所有法理途径都不会回升价值”
原标题:"Not all code paths return a value"
  • 时间:2011-10-29 02:29:30
  •  标签:
  • c#
  • methods

Im与阵列和方法合作。 Im正在撰写一部法律,计算一个阵列中用户输入的平均数字。 我先看了第一种方法,但VS告诉我,“并非所有法系途径都回升价值”。 当我用主(金)方法测试该代码时,该代码运转良好,但在我“GetValues”(GetValues)”方法中,该代码一度出现错误。

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()应该抓住阵列。

Here s the code:

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];
            }
        }
    }
}
最佳回答

Are you trying to return the integer array score?

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

因此,如果你这样说,你就可以抓住这个阵列。

int[] scores = GetValues();
问题回答

Your function is declared to return int[] but there is no single return statement within it.

在你的职责结束时,您必须返回。 编辑的错误告诉你,如果你走在GetValues(GetValues)的所有代码道路,至少还有一只字不回价值。 (return score to the end of the function)。

Your method is not returning anything and is not declared void, but int[]

The compiler error is because you need to add a return score

除此之外,你还做了一次整整整整整整群地方变量,然后与似乎像设计/地质错误那样做。 基本上说,除非你使整整整整位成员,而不是地方变量,否则该守则就可以删除。 你们也可以在第一处这样做。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签