【问题标题】:Restructuring program to meet certain criteria, could use some advice重组计划以满足某些标准,可以使用一些建议
【发布时间】:2014-08-24 09:25:05
【问题描述】:

(对不起,如果我问的问题太多了)

现在我只是想弄清楚如何重组我编写的程序以满足标准。我想将它分成不同的方法以使其更易于阅读,但我无法让不同的方法相互配合(例如变量范围错误)。

现在我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scoring {
class Program {

    static int highOccurrence = 0;
    static int lowOccurrence = 0;

    static void Main(string[] args) {
        int[] scores = { 4, 7, 9, 3, 8, 6 };


        findScore(scores);
        ExitProgram();
    }


    static int findOccurrence(int[] scores) { //find the number of times a high/low occurs

        for (int i = 0; i < scores.Length; i++) {

            if (low == scores[i]) {

                lowOccurrence++;
                //record number of time slow occurs
            }

            if (high == scores[i]) {


                highOccurrence++;

                //record number of times high occurs
            }

            }

    }

    static int findScore(int[] scores) {


        int[] arrofNormal = new int[scores.Length];

        int low = scores[0];
        int high = scores[0];
        int total = 0;


        //int highOccurrence = 0;
        //int lowOccurrence = 0;


        for (int i = 0; i < scores.Length; i++) {

           // if (low == scores[i]) {

           //     lowOccurrence++;
           //     //record number of time slow occurs
           // }

           // if (high == scores[i]) {


           //     highOccurrence++;

           //     //record number of times high occurs
           // }

            if (low > scores[i]) {

                low = scores[i];


            } //record lowest value


            if (high < scores[i]) {

                high = scores[i];

                //record highest value
            }


        }

        for (int x = 0; x < scores.Length; x++) {

            if (scores[x] != low && scores[x] != high) {

                arrofNormal[x] = scores[x];
                 //provides the total of the scores (not including the high and the low) 
            }

            total += arrofNormal[x];

        }

        if (highOccurrence > 1) { //if there is more than 1 high (or 1 low) it is added once into the total

            total += high;

            if (lowOccurrence > 1) {

                total += low;
            }
        }


        Console.WriteLine("Sum = " + total);



        return total; //remove not all code paths return.. error


    }



    static void ExitProgram() {
        Console.Write("\n\nPress any key to exit program: ");
        Console.ReadKey();
    }//end ExitProgram

}
}

如您所见,它仍然是一项非常有进展的工作。我收到诸如“变量名”在当前上下文中不存在之类的错误,我知道这是一个范围错误,我该如何解决?建议将不胜感激:)

【问题讨论】:

  • 怎么样:您负责正确格式化您的问题,没有那么多空格,所以任何试图帮助您的人都不需要浪费时间滚动?关于如何提出一个好问题,以及网站上的适当礼仪有整页,他们没有说明问题的数量,但他们确实提到了质量。

标签: c# variables methods scope


【解决方案1】:

是的,您可以在 10 秒内用 Linq 编写此代码,但我认为您正在尝试学习 C# 的基本方面,而 Linq 对此无济于事。 您的问题是您正在尝试使用不同方法的变量(即使它是静态的,也没关系)。如果您在特定范围内需要参数,可以将参数添加到 findOccurrence 方法。

private static void findOccurrence(int[] scores, int low, int high)
    { //find the number of times a high/low occurs
        for (int i = 0; i < scores.Length; i++)
        {
            if (low == scores[i])
            {
                lowOccurrence++;
                //record number of time slow occurs
            }

            if (high == scores[i])
            {
                highOccurrence++;

                //record number of times high occurs
            }
        }
    }

在 findScore() 中你可以像这样调用上面的方法:

        findOccurrence(scores, low, high);

        if (highOccurrence > 1)
        { //if there is more than 1 high (or 1 low) it is added once into the total
            total += high;

            if (lowOccurrence > 1)
            {
                total += low;
            }
        }

我希望这会按预期工作。愉快地学习 C#

【讨论】:

  • 非常感谢您的回复!这似乎成功了!但现在我收到此错误“并非所有代码路径都返回值”。现在我已经通过在方法末尾的返回值解决了这个问题,但这似乎不是一种可靠的编程方式,你有什么建议吗?
  • 哦,我完全忘记了,对不起。您可以将您的方法标记为 void(意味着它不返回任何值)。检查我的代码,我将 findOccurrence 方法定义为 void,它不必返回任何内容。您可以对 findScore 方法执行相同的操作。如果您想进入下一步的编程,您可以学习如何制作新课程。这是必不可少的东西,你肯定会需要它。
  • 再次感谢您的回复。我会将我的方法更改为 void,但此任务要求“大多数方法都是返回值”。我只是想弄清楚如何才能正确地做到这一点。关于您之前的回答,我还有一个后续问题,我还想将 findScore 的另一部分放入一个新方法中(即记录高/低值的第一个 for 循环),但我得到一个“方法'findScore'没有重载”需要 1 个参数。非常感谢您的帮助
  • 但是findOccurrence 这里并没有真正做任何事情。如果您将scores 数组和两个0 传递给它,您将一无所获。 lowhigh 的作用域是该方法,一旦执行完成,您将松开它们。您需要为您的班级全局定义它们,然后才能在班级中的任何地方访问。
【解决方案2】:

这应该让你开始:)

您需要将highlow 移出findScore 方法并使用0 而不是scores[0] 进行初始化。然后你必须在调用findOccurrence 之前调用findScore 以允许这两个变量在需要时包含你想要的值。

而且看起来您来自 Java 背景。在 C# 中,根据命名约定,所有方法都应以大写字母开头。

您可以从 Linq 中受益匪浅。例如,您可以找到 lowscores.Min()highscores.Max()

findOccurrence 可以写得更简洁:

static int FindOccurence(int[] scores)
{
    lowOccurrence = scores.Count(s => s == low);
    highOccurrence = scores.Count(s => s == high);
}

这样的事情会提高我的可读性。

这是Introduction to LINQ

【讨论】:

  • 是的,我已经使用 LINQ 解决了这个问题,在这个特定的任务中不允许使用 LINQ :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 2018-07-09
  • 2021-11-20
  • 1970-01-01
相关资源
最近更新 更多