私は、それが何であっても仕事を確認できる人を持つことの大きなファンです。エッセイであろうとプロジェクトであろうと、習慣(絵)であろうと、私はより良いものになるためのフィードバックが好きです。以下は、私が学校で働いていたプロジェクトのコードです。私はそれをかなり終えたし、そこに2つのエラーがある。エラーはMain() - GetInput()の内部にあります。参照変数nameListとplayerScoreの2つのエラーを投げていて、割り当てられていないと言っています。私は彼らが私の知る限りで割り当てられているので、なぜ、なぜわからない。しかし、それを助けてくれれば助かりますが、コメントの指示に従って、何か良いやり方をすることができるかどうかのフィードバックをもっと探しています。私は配列を使用する必要があり、値ごとにavg変数を渡している間にメソッド間の参照によってそれらを渡す必要があります。あなたが出て使用しなければならないREFを使用しているチラッからプルーフ・チェックと2つのエラー - あるメソッドから別のメソッドへの割り当てられていない変数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneDial
{
class Program
{
// Display the player names and corresponding scores
static void DisplayPlayerData(ref string[] nameList, ref int[] playerScore, ref int count)
{
for (int i = 0; i < count; i++)
{
Console.WriteLine("{0} : {1}", nameList[i], playerScore[i]);
}
}
// Calculate the average score between all players and returns it by value to Main()
static void CalculateAverageScore(ref string[] nameList, ref int[] playerScore, ref int count, double avg)
{
avg = playerScore.Average();
}
// Display all players whose score is below the average, with their corresponding scores
static void DisplayBelowAverage(ref string[] nameList, ref int[] playerScore, ref int count, double avg)
{
Console.WriteLine("Players who scored below the average:");
for (int i = 0; i < count; i++)
{
if (playerScore[0] < avg)
Console.WriteLine("{0}:{1}", nameList, playerScore);
count++;
}
}
// Get player names and their scores and stores them into array for an unknown number of players up to 100
static void InputData(ref string[] nameList, ref int[] playerScore, ref int count)
{
string userInput;
nameList = new string [100];
playerScore= new int [100];
do
{
Console.Write("\nEnter a players name: ");
userInput = Console.ReadLine();
if (userInput != "Q" && userInput != "q")
{
nameList[0] = Console.ReadLine();
++count;
}
else break;
Console.WriteLine("Enter {0}'s score:", userInput);
playerScore[0] = Convert.ToInt32(Console.ReadLine());
} while (userInput != "Q" && userInput != "q");
}
//Declare variables for number of players and average score and two arrays of size 100 (one for names, one for respective scores
//Calls functions in sequence, passing necessary parameters by reference
static void Main(string[] args)
{
string[] nameList;
int[] playerScore;
int count = 0;
double avg = 0;
//InputData(), passing arrays and number of players variable by reference
//******nameList and playerScore are throwing errors; use of unassigned local variables********
InputData(ref nameList, ref playerScore, ref count);
//DisplayPlayerData(), passing arrays and number of players by reference
DisplayPlayerData(ref nameList, ref playerScore, ref count);
//CalculateAverageScore(), passing arrays and number of players by reference. Store returned value in avg variable
CalculateAverageScore(ref nameList, ref playerScore, ref count, avg);
//DisplayBelowAverage(), passing arrays and number of players variable by reference, passing average variable by value
DisplayBelowAverage(ref nameList, ref playerScore, ref count, avg);
}
}
}
コード内のすべてのコメント(私のエラーコメントを除く)は、このラボで提供された疑似コードから直接得られたものです。これが私が参考ルートを試していた理由です。私が家に帰るとき、私は間違いなくこれを裂くでしょう。 – user1174357
Ken2kのように初期のエラーを修正しました.Outは初期化を必要としませんが、文字列[]またはint []を初期化したい場合は、空の初期化子{}を使うことができます。あなたがそれを宣言したい最初の長さを知らないときにも.. Resize(ref ...)からあなたを離れて常に新しいものにしています。それはちょうど私の好みです..あなたの研究室の仕事で幸運 – MethodMan