Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保龄球项目中的C#数组。不会使用最低分数_C#_Arrays - Fatal编程技术网

保龄球项目中的C#数组。不会使用最低分数

保龄球项目中的C#数组。不会使用最低分数,c#,arrays,C#,Arrays,我正在尝试创建一个保龄球程序,当你输入你的名字,然后输入你的分数时,它将取球员的平均分、最低分和最高分并打印出来。然而,由于某种原因,我无法得到最低分数来打印,因为当我输入两次时,它将使用空白值而不是最低输入值和名称。如何修复此问题,使其显示最低分数 { class Program { static void Main(string[] args) { const int SIZE = 10; int i; // create an

我正在尝试创建一个保龄球程序,当你输入你的名字,然后输入你的分数时,它将取球员的平均分、最低分和最高分并打印出来。然而,由于某种原因,我无法得到最低分数来打印,因为当我输入两次时,它将使用空白值而不是最低输入值和名称。如何修复此问题,使其显示最低分数

{    
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;
   }
{
班级计划
{
静态void Main(字符串[]参数)
{
常数int SIZE=10;
int i;
//创建一个包含10个元素的数组
string[]scoreInfo=新字符串[大小];
字符串[]名称=新字符串[大小];
int[]分数=新int[大小];
对于(i=0;i
}
}

如下修改平均函数:-

static double AverageScore(int[] numbers, int j)
{
   double sum = 0;
   for (int i = 0; i < j; i++)
   {
       sum += numbers[i];
   }
   return (double)sum / j;
}
static int LowScore(int[] scores, int j)
{
     int min = scores.Where((v, i) => i < j).Min();
     return Array.IndexOf(scores, min);
}
Console.WriteLine("{0} scored the lowest at {1}", names[LowScore(scores, i)], scores[LowScore(scores, i)]);

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

从一本将名字映射到分数的字典开始,这样你就不必乱搞索引了

那么,林克是你的朋友,你已经注意到了。您不需要为已经存在的对象(如min/max/average)创建函数

例如

字典排名=新建字典();
添加(“adam”,20);
添加(“法案”,10);
排名。添加(“卡尔”,30);
双平均=排名平均值(kvp=>(双)kvp.值);
var sorted=ranking.OrderBy(kvp=>kvp.Value);
var min=sorted.First();
var max=sorted.Last();
WriteLine(“平均值:{0}”,平均值);
WriteLine(“最低:{0}和{1}”,最小键,最小值);
WriteLine(“最高:{0}和{1}”,最大键,最大值);

真棒的男人。很好用。现在,如果我想把业务逻辑放到一个类中,我该怎么做呢?@GoodyGoodmansen-什么样的业务逻辑。。?有几件事我注意到了,你应该使用
try-catch
处理异常,你不应该再次调用方法&如果没有必要,最好将其存储到变量中。啊,我明白了。试着接球,我得研究一下。再次感谢你帮我弄明白逻辑。
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);