重组方案要达到一定的标准,可以使用一些建议
本文关键字:可以使 标准 方案 重组 | 更新日期: 2023-09-27 18:19:12
(抱歉,如果我问的太多了)
现在我只是想弄清楚如何重组我写的程序以满足标准。我想把它分解成不同的方法,使它更容易阅读,但我有困难让不同的方法相互发挥(例如。变量作用域错误)。
现在我的代码如下:
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
}
}
正如你所看到的,它仍然是一个非常进步的工作。我得到错误,如"变量名"是不存在于当前上下文中",我知道这是一个范围错误,我怎么能修复它?如有建议,不胜感激:)
是的,你可以在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#愉快
这应该可以让你开始:)
您需要将high
和low
移到findScore
方法之外,并初始化为0而不是scores[0]
。然后,在调用findOccurrence
之前,您必须调用findScore
,以允许这两个变量在需要时包含您想要的值。
而且看起来你有Java背景。在c#中,根据命名约定,所有方法都应该以大写字母开头。
你可以从Linq中获益良多。例如,您可以找到low
与scores.Min()
, high
与scores.Max()
。
findOccurrence
可以写得更简短:
static int FindOccurence(int[] scores)
{
lowOccurrence = scores.Count(s => s == low);
highOccurrence = scores.Count(s => s == high);
}
这样的事情会提高我的可读性。
LINQ简介