简体   繁体   中英

C# Arrays in a Bowling Program. Wont use Lowest Score

I am trying to create a bowling program that when you enter in your name followed by your score, it will take the average, lowest, and highest scores of the players and print them. However for some reason I cannot get the lowest score to print, as when I hit enter twice, it will use the blank value instead of the lowest entered value and name. How can I fix this so that it will display the lowest score?

{    
class Program
{
   static void Main(string[] args)
   {
       const int SIZE = 10;
       int i;

       // create an array with 10 elements
       string[] scoreInfo = new string[SIZE];
       string[] names = new string[SIZE];
       int[] scores = new int[SIZE];


       for (i = 0; i < SIZE; i++)
       {
           // Prompt the user
           Console.Write("Enter your first name and score on one line");
           Console.WriteLine(" separated by a space.");

           // Read one line of data from the file and save it in inputStr
           string inputStr = Console.ReadLine( );
           // if statement to break when the user enters a zero
           if (inputStr == String.Empty)
           {
               break;
           }
           // The Split method creates an array of two strings
           scoreInfo = inputStr.Split();
           // Parse each element of the array into the correct data type
           names[i] = scoreInfo[0];
           scores[i] = int.Parse(scoreInfo[1]);
       }


       Console.WriteLine("The avarage score is {0}", AverageScore(scores, i));
       Console.WriteLine("{0} scored the lowest at {1}", names[LowScore(scores, i--)], scores[LowScore(scores, i--)]);
       Console.WriteLine("{0} scored the highest at {1}", names[HighScore(scores)], scores[HighScore(scores)]);
       Console.ReadLine();


       Console.ReadLine();
   }

   static int LowScore(int[] scores, int j)
   {
       int min = scores.Min();
       return Array.IndexOf(scores, min);
   }

   static int HighScore(int[] scores)
   {
       int max = scores.Max();
       return Array.IndexOf(scores, max);
   }

   static double AverageScore(int[] numbers, int j)
   {
       double average = 0;
       for (int i = 0; i < j--; i++)
       {
           int product = 1;
           product = numbers[i] * product;
           average = product / j;
       }
       return average;
   }

} }

Use a different data structure and make less work for yourself.

Start with a dictionary that maps names to scores so that you don't have to mess around with indexes.

Then, LINQ is your friend, as you've already noticed. You don't need to create functions for things that already exist (like min/max/average).

eg.

Dictionary<string, int> ranking = new Dictionary<string, int>();
ranking.Add("adam", 20);
ranking.Add("bill", 10);
ranking.Add("carl", 30);

double avg = ranking.Average(kvp => (double)kvp.Value);
var sorted = ranking.OrderBy(kvp => kvp.Value);
var min = sorted.First();
var max = sorted.Last();

Console.WriteLine("Average: {0}", avg);
Console.WriteLine("Lowest: {0} with {1}", min.Key, min.Value);
Console.WriteLine("Highest: {0} with {1}", max.Key, max.Value);

Modify your Average function like this:-

static double AverageScore(int[] numbers, int j)
{
   double sum = 0;
   for (int i = 0; i < j; i++)
   {
       sum += numbers[i];
   }
   return (double)sum / j;
}

I am not sure why you were taking product of items for finding average of scores.

And Your Min function like this:-

static int LowScore(int[] scores, int j)
{
     int min = scores.Where((v, i) => i < j).Min();
     return Array.IndexOf(scores, min);
}

Here, you are passing the complete array of integer, so if only 3 players entered values, rest of the values will be initialized to default , ie 0 for int , Thus you were getting 0 as Min value.

Also, You need to change the method invocation like this:-

Console.WriteLine("{0} scored the lowest at {1}", names[LowScore(scores, i)], scores[LowScore(scores, i)]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM