我为长调用方法道歉,但我仍然是C#的新手,并且想知道如何为我的变量设置数组,所以没有太多浪费的编码。还有一件坏事就是不会读取文本文件,这些文件有这样的行“28,m,s,3”
namespace project2
{
class Program
{
static void Main(string[] args)
{
int livingIn = 0;
int dist1 = 0;
int dist2 = 0;
int dist3 = 0;
int dist4 = 0;
int dist5 = 0;
int dist6 = 0;
int dist7 = 0;
int dist8 = 0;
int dist9 = 0;
int dist10 = 0;
int dist11 = 0;
int dist12 = 0;
int dist13 = 0;
int dist14 = 0;
int dist15 = 0;
int dist16 = 0;
int dist17 = 0;
int dist18 = 0;
int dist19 = 0;
int dist20 = 0;
int dist21 = 0;
int dist22 = 0;
int age = 0;
int UnderEighteen = 0;
int NineteenToThirty = 0;
int ThirtyoneToFortyfive = 0;
int FortysixToSixtyfour = 0;
int SixtyfiveAndUp = 0;
textfilereader(livingIn,age,dist1,dist2,dist3,dist4,dist5,dist6,dist7,dist8,dist9,dist10,dist11,dist12,dist13,dist14,dist15,dist16,dist17,dist18,dist19,dist20,dist21,dist22,UnderEighteen,NineteenToThirty,ThirtyoneToFortyfive,FortysixToSixtyfour,SixtyfiveAndUp);
}
static void textfilereader(int livingIn, int dist1, int dist2, int dist3, int dist4, int dist5, int dist6, int dist7, int dist8, int dist9, int dist10, int dist11, int dist12, int dist13, int dist14, int dist15, int dist16, int dist17, int dist18, int dist19, int dist20, int dist21, int dist22, int age, int UnderEighteen, int NineteenToThirty, int ThirtyoneToFortyfive, int FortysixToSixtyfour, int SixtyfiveAndUp)
{
FileStream fstream = new FileStream("project2.txt", FileMode.Open, FileAccess.Read);
StreamReader infile = new StreamReader(fstream);
string inputrecord = "";
string[] fields;
inputrecord = infile.ReadLine();
while (inputrecord != null)
{
fields = inputrecord.Split(',');
if (int.TryParse(fields[0], out age))
{
agegroupcounter(age,UnderEighteen,NineteenToThirty,ThirtyoneToFortyfive,FortysixToSixtyfour,SixtyfiveAndUp);
}
if (int.TryParse(fields[3], out livingIn))
{
districtCounter(livingIn,dist1, dist2, dist3, dist4, dist5, dist6, dist7, dist8, dist9, dist10, dist11, dist12, dist13, dist14, dist15, dist16, dist17, dist18, dist19, dist20, dist21, dist22);
}
inputrecord = infile.ReadLine();
}
Finalcount(dist1,dist2,dist3,dist4,dist5,dist6,dist7,dist8,dist9,dist10,dist11,dist12,dist13,dist14,dist15,dist16,dist17,dist18,dist19,dist20,dist21,dist22,UnderEighteen,NineteenToThirty,ThirtyoneToFortyfive,FortysixToSixtyfour,SixtyfiveAndUp);
}
static void Finalcount(int dist1, int dist2, int dist3, int dist4, int dist5, int dist6, int dist7, int dist8, int dist9, int dist10, int dist11, int dist12, int dist13, int dist14, int dist15, int dist16, int dist17, int dist18, int dist19, int dist20, int dist21, int dist22, int UnderEighteen, int NineteenToThirty, int ThirtyoneToFortyfive, int FortysixToSixtyfour, int SixtyfiveAndUp)
{
Console.WriteLine("The age group tallies are");
Console.WriteLine(" Eighteen and under = {0}", UnderEighteen);
Console.WriteLine(" Nineteen to thirty = {0}", NineteenToThirty);
Console.WriteLine(" Thirtyone to fortyfive = {0}", ThirtyoneToFortyfive);
Console.WriteLine(" fortysix to sixtyfour = {0}", FortysixToSixtyfour);
Console.WriteLine("Sixtyfive and older = {0}", SixtyfiveAndUp);
Console.WriteLine("");
Console.WriteLine("The total of persons living in each district");
Console.WriteLine("Distict 1 = {0}",dist1);
Console.WriteLine("Distict 2 = {0}", dist2);
Console.WriteLine("Distict 3 = {0}", dist3);
Console.WriteLine("Distict 4 = {0}", dist4);
Console.WriteLine("Distict 5 = {0}", dist5);
Console.WriteLine("Distict 6 = {0}", dist6);
Console.WriteLine("Distict 7 = {0}", dist7);
Console.WriteLine("Distict 8 = {0}", dist8);
Console.WriteLine("Distict 9 = {0}", dist9);
Console.WriteLine("Distict 10 = {0}", dist10);
Console.WriteLine("Distict 11 = {0}", dist11);
Console.WriteLine("Distict 12 = {0}", dist12);
Console.WriteLine("Distict 13 = {0}", dist13);
Console.WriteLine("Distict 14 = {0}", dist14);
Console.WriteLine("Distict 15 = {0}", dist15);
Console.WriteLine("Distict 16 = {0}", dist16);
Console.WriteLine("Distict 17 = {0}", dist17);
Console.WriteLine("Distict 18 = {0}", dist18);
Console.WriteLine("Distict 19 = {0}", dist19);
Console.WriteLine("Distict 20 = {0}", dist20);
Console.WriteLine("Distict 21 = {0}", dist21);
Console.WriteLine("Distict 22 = {0}", dist22);
}
static void districtCounter(int livingIn,int dist1,int dist2,int dist3,int dist4,int dist5,int dist6,int dist7,int dist8,int dist9,int dist10,int dist11,int dist12,int dist13,int dist14,int dist15,int dist16,int dist17,int dist18,int dist19,int dist20,int dist21,int dist22)
{
if (livingIn <= 0 && livingIn >= 23)
{
cancel();
}
else
if (livingIn == 1)
{
dist1 = dist1 + 1;
}
else
if (livingIn == 2)
{
dist2 = dist2 + 1;
}
else
if (livingIn == 3)
{
dist3 = dist3 + 1;
}
else
if (livingIn == 4)
{
dist4 = dist4 + 1;
}
else
if (livingIn == 5)
{
dist5 = dist5 + 1;
}
else
if (livingIn == 6)
{
dist6 = dist6 + 1;
}
else
if (livingIn == 7)
{
dist7 = dist7 + 1;
}
else
if (livingIn == 8)
{
dist8 = dist8 + 1;
}
else
if (livingIn == 9)
{
dist9 = dist9 + 1;
}
else
if (livingIn == 10)
{
dist10 = dist10 + 1;
}
else
if (livingIn == 11)
{
dist11 = dist11 + 1;
}
else
if (livingIn == 12)
{
dist12 = dist12 + 1;
}
else
if (livingIn == 13)
{
dist13 = dist13 + 1;
}
else
if (livingIn == 14)
{
dist14 = dist14 + 1;
}
else
if (livingIn == 15)
{
dist15 = dist15 + 1;
}
else
if (livingIn == 16)
{
dist16 = dist16 + 1;
}
else
if (livingIn == 17)
{
dist17 = dist17 + 1;
}
else
if (livingIn == 18)
{
dist18 = dist18 + 1;
}
else
if (livingIn == 19)
{
dist19= dist19 + 1;
}
else
if (livingIn == 20)
{
dist20 = dist20 + 1;
}
else
if (livingIn == 21)
{
dist21 = dist21 + 1;
}
else
{
dist22 = dist22 + 1;
}
}
static void agegroupcounter(int age, int UnderEighteen, int NineteenToThirty, int ThirtyoneToFortyfive, int FortysixToSixtyfour, int SixtyfiveAndUp)
{
if (age < 0)
{
cancel();
}
else
if (age <= 18)
{
UnderEighteen = UnderEighteen + 1;
}
else
if (age >= 19 || age <= 30)
{
NineteenToThirty = NineteenToThirty + 1;
}
else
if (age >=31 || age <= 45)
{
ThirtyoneToFortyfive = ThirtyoneToFortyfive + 1;
}
else
if (age >=46 || age <= 64)
{
FortysixToSixtyfour = FortysixToSixtyfour + 1;
}
else
{
SixtyfiveAndUp = SixtyfiveAndUp + 1;
}
}
static void cancel()
{
Console.WriteLine("Invalid Data Was Entered please check your text file for any irregularities");
}
}
}
我提供了文本文件数组,但没有正确使用某些东西,因为计数方法没有计数,并且在最终计数方法中保持为零,我在其中显示了文本文件的结果。
答案 0 :(得分:3)
正如Rob在评论" there's not much we can do to help without doing a complete re-write of your code"
中所建议的那样但我想通过提示来更多地指导您简化代码:
使用int[]
替代dist1
到dist22
并将其设为全局,以便您可以避免传递数组并保持其数量。
public static int[] dist = new int[22] ; // use Enumerable.Repeat(x, 22).ToArray(); where x is an integer
// This will give you an integer array, which are all initialized with 0
使用Dictionary<int,int>
保留AgeGroups,将组的最大数量作为键,最初计为0。这可能看起来像:
public static Dictionary<int,int> AgeGroups = new Dictionary<int,int>();
// initialize
AgeGroups.Add(18, 0); // for age group under 18
AgeGroups.Add(30, 0);// for age group under 30
AgeGroups.Add(40, 0);
AgeGroups.Add(50, 0);
AgeGroups.Add(60, 0);
这样你就可以重新编写函数agegroupcounter
,如下所示:
static void agegroupcounter(int age)
{
if (age < 0)
{
cancel();
}
else
{
var index= AgeGroups.FirstOrDefault(x => x.Key < age).Key ;
AgeGroups[index] += 1;
}
}
districtCounter
的签名将是:
static void districtCounter(int livingIn)
{
if (livingIn <= 0 && livingIn >= 23)
{
cancel();
}
else
dist[livingIn] += 1;
}
最后Finalcount
看起来像是:
static void Finalcount()
{
Console.WriteLine("The age group tallies are");
foreach (var item in AgeGroups )
{
Console.WriteLine(" under {0} = {1}", item.Key,item.Value);
}
Console.WriteLine("The total of persons living in each district");
foreach (int d in dist)
{
Console.WriteLine("Distict {0} = {1}", d,d);
}
}
注意:希望这些提示可以帮助您简化代码,并将它们集成到代码中。祝你有愉快的一天
答案 1 :(得分:0)
试试这个:
public enum AgeGroup
{
UnderEighteen,
NineteenToThirty,
ThirtyoneToFortyfive,
FortysixToSixtyfour,
SixtyfiveAndUp,
}
class Program
{
static void Main(string[] args)
{
int[] dists = new int[22];
Dictionary<AgeGroup, int> ageGroups = new Dictionary<AgeGroup, int>()
{
{ AgeGroup.UnderEighteen, 0 },
{ AgeGroup.NineteenToThirty, 0 },
{ AgeGroup.ThirtyoneToFortyfive, 0 },
{ AgeGroup.FortysixToSixtyfour, 0 },
{ AgeGroup.SixtyfiveAndUp, 0 },
};
textfilereader(dists, ageGroups);
}
static void textfilereader(int[] dists, Dictionary<AgeGroup, int> ageGroups)
{
foreach (var inputrecord in File.ReadAllLines("project2.txt"))
{
string[] fields = inputrecord.Split(',');
int age;
if (int.TryParse(fields[0], out age))
{
agegroupcounter(age, ageGroups);
}
int livingIn;
if (int.TryParse(fields[3], out livingIn))
{
districtCounter(livingIn, dists);
}
}
Finalcount(dists, ageGroups);
}
static void Finalcount(int[] dists, Dictionary<AgeGroup, int> ageGroups)
{
Console.WriteLine("The age group tallies are");
Console.WriteLine(" Eighteen and under = {0}", ageGroups[AgeGroup.UnderEighteen]);
Console.WriteLine(" Nineteen to thirty = {0}", ageGroups[AgeGroup.NineteenToThirty]);
Console.WriteLine(" Thirtyone to fortyfive = {0}", ageGroups[AgeGroup.ThirtyoneToFortyfive]);
Console.WriteLine(" fortysix to sixtyfour = {0}", ageGroups[AgeGroup.FortysixToSixtyfour]);
Console.WriteLine("Sixtyfive and older = {0}", ageGroups[AgeGroup.SixtyfiveAndUp]);
Console.WriteLine("");
Console.WriteLine("The total of persons living in each district");
for (var i = 0; i < dists.Length; i++)
{
Console.WriteLine("Distict {0} = {1}", i + 1, dists[i]);
}
}
static void districtCounter(int livingIn, int[] dists)
{
if (livingIn <= 0 && livingIn >= 23)
{
cancel();
}
else
{
dists[livingIn - 1]++;
}
}
static void agegroupcounter(int age, Dictionary<AgeGroup, int> ageGroups)
{
if (age < 0)
{
cancel();
}
else if (age <= 18)
{
ageGroups[AgeGroup.UnderEighteen]++;
}
else if (age >= 19 || age <= 30)
{
ageGroups[AgeGroup.NineteenToThirty]++;
}
else if (age >= 31 || age <= 45)
{
ageGroups[AgeGroup.ThirtyoneToFortyfive]++;
}
else if (age >= 46 || age <= 64)
{
ageGroups[AgeGroup.FortysixToSixtyfour]++;
}
else
{
ageGroups[AgeGroup.SixtyfiveAndUp]++;
}
}
static void cancel()
{
Console.WriteLine("Invalid Data Was Entered please check your text file for any irregularities");
}
}
我没有测试过,但值得一看。