Thursday, November 27, 2014

how do i split an array of strings and ints.

you can try to use the following:



class Program
{

static void Main(string[] args)
{
// declare and array of integers
string[] name = new string[10];
int[] score = new int[10];

Console.WriteLine("\nSaturday Coder's Bowling Team");
Console.WriteLine("Enter in a name and score for each person on the team.");
Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.\n");

// fill an array with user input
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine("Enter in a name and score: ");
string line = Console.ReadLine();

name[i] = line.Substring(0,line.IndexOf(' '));
score[i] = int.Parse(line.Substring(line.IndexOf(' ') + 1));
}

Console.WriteLine("------------ Input Complete ------------\n");
Console.WriteLine("Here are the scores for this game:");

for (int i = 0; i < score.Length; i++)
{
Console.WriteLine("{0}'s score was {1}.", name[i],score[i]);
}

Console.WriteLine();
HighScore(score,name);
LowScore(score,name);
AverageScore(score);

Console.WriteLine("Press Enter to continue. . .");
Console.ReadLine();
}

static void HighScore(int[] score,string[] name)
{
int max = score.Max();
for (int i = 0; i < score.Length; i++)
{
if (score[i] == max)
{
Console.WriteLine("Congratulations {0}, your score of {1} was the highest.", name[i],score[i]);
}
}
}

static void LowScore(int[] score,string[] name)
{
int min = score.Min();
for (int i = 0; i < score.Length; i++)
{
if (score[i] == min)
{
Console.WriteLine("{0}, your score of {1} was the lowest. Better get some practice.", name[i],score[i]);
}
}
}

static void AverageScore(int[] score)
{
int sum = score.Sum();
int average = sum / score.Length;
Console.WriteLine("The average score for this game was {0:d}.", average);
}
}


No comments:

Post a Comment