我正在尝试在C#Visual Studio 2017中创建一个表。在使用Visual Studio时,我有点绿色。
我打算使用CSV。文件
我希望桌子看起来像这样:
firstName + secondName以及该人捐赠的金额。
我试图让我们在课堂上使用的东西变得简单
最后,我想将这些数字加在一起,这样我就能得到记事本文件中所有这些数字的总和,同时我想在数字旁边显示名字旁边最大数字的人,以及最后只是显示所有人的名字+第二个名字+数字但只有名字旁边有150或以上的人
这是我之前制作的简单布局,但我正在努力将2个单独的名称/单词组合在一起+数字。
static void Main(string[] args)
{
string fileIn = "C:/quiz-results.csv";//reads in the file from the H drive
if (File.Exists(fileIn))
{
//filefound
string[] allLines = File.ReadAllLines(fileIn);
string[] names = new string[allLines.Length];// creates array for the data that is going to be used in this program
int[] scores = new int[allLines.Length];
string[] quizResults = new string[allLines.Length];//array to store the final result
for (int i = 0; i < allLines.Length; i++)
{
string[] lineSpilt = allLines[i].Split(',');
names[i] = lineSpilt[0];
scores[i] = Convert.ToInt32(lineSpilt[1]);
}
for (int i = 0; i < scores.Length; i++)//loops through the whole array scores
{
//compute result
quizResults[i] = ComputeResult(scores[i]);
}
// output to console
OutputResultsToConsole(names, quizResults);
// find best score
int bestScore = GetBestScore(scores);
Console.WriteLine("the best overall score was:{0}", bestScore);
//find worst score
int worstScore = GetWorstSCore(scores);
Console.WriteLine("the worst overall score was:{0}", worstScore);
//find worst score index and name
int worstScoreIndex = GetWorstSCoreIndex(scores);
Console.WriteLine("the worst overall score was:{0} ({1})", scores[worstScoreIndex], names[worstScoreIndex]);
//number of passes
int numberOfPasses = GetNumberOfPasses(quizResults);
Console.WriteLine("the number of passes were: {0}", numberOfPasses);
//number of fails
int numberOfFails = GetNumberOfFails(quizResults);
Console.WriteLine("the number of fails were: {0}", numberOfFails);
// get average score
int averageScore = ComputeAverageOverallScore(scores);
Console.WriteLine("the average quiz score was : {0}", averageScore);
// scored less than 12
Console.WriteLine("Scored less than 12");
for (int i = 0; i < scores.Length; i++)
{
if (scores[i] < 12)
{
Console.WriteLine("{0} {1}", names[i], scores[i]);
}
}
// output to txt file
string fileOut = "E:/results-2016-final.txt";
OutputResultsToTxtFile(fileOut, names, quizResults, bestScore, worstScore, numberOfPasses, numberOfFails, averageScore);
}
Console.ReadLine();
}
private static void OutputResultsToTxtFile(string fileOut, string[] names, string[] quizResults, int bestScore, int worstScore, int numberOfPasses, int numberOfFails, int averageScore)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Results 2016 - Final");
// individaul restults
for (int i = 0; i < names.Length; i++)
{
sb.AppendLine($"{names[i],-20}: {quizResults[i]}");
}
// summary data // all display on the text file
sb.AppendLine();
sb.AppendLine("Summary Data");
sb.AppendLine($"Highest Score: {bestScore}");
sb.AppendLine($"Lowest Score : {worstScore}");
sb.AppendLine($"Number Of Passes : {numberOfPasses}");
sb.AppendLine($"Number Of Fails : {numberOfFails}");
sb.AppendLine($"Average Score : {averageScore}");
sb.AppendLine("----------------- END OF FILE ----------------");
// write data to file
using (StreamWriter sw = new StreamWriter(fileOut))
{
sw.Write(sb);
}
}
private static int ComputeAverageOverallScore(int[] scores)
{
int sum = 0;
for (int i = 0; i < scores.Length; i++)//lopping through the lenght of the array
{
sum += scores[i];//+= means add the two values to sum // i++ is the same as i+=1 or i+1
}
int average = sum / scores.Length;
return average;
}
private static int GetNumberOfFails(string[] quizResults)
{
int count = 0;// declaring a variable of the type int and asigns it the value of 0
foreach (string i in quizResults)// loops through the whole array looking for strings
{
if (i == "fail")
{
count++;//add one to count
}
}
return count;// returns the count value
}
private static int GetNumberOfPasses(string[] quizResults)
{
int count = 0;// declaring a variable of the type int and asigns it the value of 0
foreach (string i in quizResults)// loops through the whole array looking for strings
{
if (i == "pass")
{
count++;//add one to count
}
}
return count;// returns the count value
}
private static int GetWorstSCore(int[] scores)
{
int worstScore = scores[0];
for (int i = 1; i < scores.Length; i++)
{
if (scores[i] < worstScore)
{
worstScore = scores[i];
}
}
return worstScore;
}
private static int GetWorstSCoreIndex(int[] scores)//name
{
int index = 0;
int worstScore = scores[index];
for (int i = 1; i < scores.Length; i++)
{
if (scores[i] < worstScore)
{
worstScore = scores[i];
index = i;
}
}
return index;
}
private static int GetBestScore(int[] scores)
{
int bestScore = scores[0];
for (int i = 1; i < scores.Length; i++)
{
if (scores[i] > bestScore)
{
bestScore = scores[i];
}
}
return bestScore;
}
private static string ComputeResult(int score)// finds out of the student has passed
{
if (score >= PASS_MARK)
{
return "pass";
}
else
{
return "fail";
}
}
private static void OutputResultsToConsole(string[] names, string[] quizResults)// output the names and there reults to the console
{
Console.WriteLine("results 2016");
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("{0,-12} {1} ", names[i], quizResults[i]);
}
}
}
}