【问题标题】:C# Arrays in a Bowling Program. Wont use Lowest Score保龄球程序中的 C# 数组。不会使用最低分
【发布时间】:2014-11-24 14:05:53
【问题描述】:

我正在尝试创建一个保龄球程序,当您输入您的姓名后跟您的分数时,它将获取球员的平均分、最低分和最高分并打印出来。但是由于某种原因,我无法获得要打印的最低分数,因为当我按两次 Enter 时,它将使用空白值而不是输入的最低值和名称。我该如何解决这个问题,以便它显示最低分?

{    
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;
   }

} }

【问题讨论】:

    标签: c# arrays


    【解决方案1】:

    使用不同的数据结构,减少自己的工作量。

    从将名称映射到分数的字典开始,这样您就不必弄乱索引了。

    那么,正如您已经注意到的,LINQ 是您的朋友。您不需要为已经存在的事物创建函数(例如 min/max/average)。

    例如。

    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);
    

    【讨论】:

      【解决方案2】:

      像这样修改你的平均函数:-

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

      我不确定您为什么要使用项目乘积来求平均分。

      你的 Min 函数是这样的:-

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

      在这里,您传递的是完整的整数数组,因此如果只有 3 个玩家输入值,其余值将初始化为 default,即 0int,因此您得到 0 作为 Min价值。

      另外,您需要像这样更改方法调用:-

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

      【讨论】:

      • 很棒的人。工作得很好。现在,如果我想将业务逻辑放入一个类中,我该怎么做呢?
      • @GoodyGoodmansen - 什么样的业务逻辑..?我注意到的事情很少,您应该使用try-catch 处理异常,如果没有必要,您不应该再次调用方法,最好将其存储到变量中。
      • 啊,我想通了。和try-catch,我将不得不研究它。以太方式再次感谢您帮助我弄清楚逻辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多