ここで私のコードです。私はtxtファイルからデータを引き出し、Ageと経験の平均を持つ新しいファイルに書き込む。私は正しい平均を得ていない。私のミスはどこですか?合計の計算上の平均を
class Program
{
static int age, exp, id, ageSum = 0, expSum = 0, empSum = 0;
static double avgAge, avgExp;
static char type;
const string INPUT_FILE_NAME =
"\\Users\\Jon.Jon-PC\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication9\\ConsoleApplication9\\data.txt";
const string OUTPUT_FILE_NAME =
"\\Users\\Jon.Jon-PC\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication9\\ConsoleApplication9\\dataout.txt";
static StreamReader fileIn;
static StreamWriter fileOut;
static string lineIn, eligibility;
static void OpenFiles()
{
if (File.Exists(INPUT_FILE_NAME))
{
fileIn = File.OpenText(INPUT_FILE_NAME);
Console.WriteLine("{0} was opened", INPUT_FILE_NAME);
}
else
{
Console.WriteLine("Error: {0} does not exit\n",
INPUT_FILE_NAME);
Environment.Exit(0);
}
fileOut = File.CreateText(OUTPUT_FILE_NAME);
if (File.Exists(OUTPUT_FILE_NAME))
Console.WriteLine("{0} was created\n",
OUTPUT_FILE_NAME);
else
{
Console.WriteLine("Error: {0} could not be created\n",
OUTPUT_FILE_NAME);
Environment.Exit(0);
}
}
static void ParseLineIn()
{
string[] words = new string[4];
lineIn = lineIn.Trim();
while (Regex.IsMatch(lineIn, "[ ]{2}"))
lineIn = lineIn.Replace(" ", " ");
words = lineIn.Split(' ');
id = int.Parse(words[0]);
type = char.Parse(words[1]);
age = int.Parse(words[2]);
exp = int.Parse(words[3]);
}
static void UpdateTotals()
{
empSum++;
ageSum += age;
expSum += exp;
}
static void CalcAvg()
{
avgAge = ageSum/empSum;
avgExp = expSum/empSum;
}
static void PrintAvg()
{
fileOut.Write("Avg\t {0} {1}", avgAge, avgExp);
}
static void CalcRetirement()
{
switch (type)
{
case 'm':
case 'M':
if (age < 55 && exp < 20)
eligibility = ("lack of experience age");
else if (age >= 55 && exp >= 20)
eligibility = ("can retire");
else if (age >= 55 && exp < 20)
eligibility = ("lack of experience");
else if (age < 55 && exp >= 20)
eligibility = ("underage");
else
eligibility = ("Your entry is invalid");
break;
case 'w':
case 'W':
if (age < 63 && exp < 25)
eligibility = ("lack of exp age");
else if (age >= 63 && exp >= 25)
eligibility = ("can retire");
else if (age >= 63 && exp < 25)
eligibility = ("lack of exp");
else if (age < 63 && exp >= 25)
eligibility = ("lack age");
else
eligibility = ("Your entry is invalid");
break;
case 's':
case 'S':
if (age < 60 && exp < 24)
eligibility = ("lack of exp age");
else if (age >= 60 && exp >= 24)
eligibility = ("can retire");
else if (age >= 60 && exp < 24)
eligibility = ("lack of exp");
else if (age < 60 && exp >= 24)
eligibility = ("underage");
else
eligibility = ("Your entry is invalid");
break;
}
}
static void CloseFiles()
{
fileIn.Close(); fileOut.Close();
}
static void PrintReportHeadings()
{
fileOut.WriteLine(" Employee Report ");
fileOut.WriteLine();
fileOut.WriteLine(" ID Age Exp Eligibility ");
fileOut.WriteLine("---- --- --- ----------- ");
}
static void printDetailLine()
{
fileOut.WriteLine("{0} {1} {2} {3}", id, age, exp, eligibility);
}
static void Main(string[] args)
{
OpenFiles();
PrintReportHeadings();
while ((lineIn = fileIn.ReadLine()) != null)
{
UpdateTotals();
ParseLineIn();
CalcRetirement();
printDetailLine();
}
CalcAvg();
PrintAvg();
CloseFiles();
}
}
私は非常にコーディングに新しいので、私は今学んだもの以外に他の多くを知らない。私が引っ張っているファイルのデータは以下の通りです。 Imは、A列とE列の平均を計算しています。平均は私が何をしないのです57.0と27.5 ことになっている
Employee Report
ID Age Exp Eligibility
---- --- --- -----------
1235 45 20 lack of exp age
2536 55 21 lack of exp age
5894 60 30 lack age
4597 75 35 can retire
2597 35 10 lack of exp age
5689 40 20 lack of exp age
5489 55 39 lack age
5872 60 40 can retire
5569 55 25 can retire
5566 80 20 lack of exp
8865 59 35 can retire
5598 65 35 can retire
Avg 51 24
私の現在の出力は以下の通りです
A E
1235 W 45 20
2536 W 55 21
5894 W 60 30
4597 W 75 35
2597 S 35 10
5689 S 40 20
5489 W 55 39
5872 M 60 40
5569 M 55 25
5566 W 80 20
8865 M 59 35
5598 S 65 35
..?
「empSum」、「ageSum」、および「expSum」の診断を適切なタイミングで印刷すると、問題をデバッグするのに便利です。ノイズを導入せずに問題を再現するために必要なものだけに絞り込まれていないプログラム全体をダンプしても、他の人にはあまり面倒ではありません。興味深いのは、印刷している平均値が13データ点に対して整数であり、13データ行に12データ点があることです。 – mah
[mcve]を見てください – Claudius
あなたは 'UpdateTotals()'の前に 'ParseLineIn()'を呼び出すか、最後の年齢を追加しません。また、行にage/expデータがあるかどうかにかかわらず、 'empSum'をインクリメントしています(つまり、ファイルにヘッダーや空白行がある場合は1にインクリメントします。私は他にも問題があるように感じますが、そこから始めてください。 – Quantic