C#保龄球阵列程序

本文关键字:程序 阵列 保龄球 | 更新日期: 2023-09-27 18:28:02

所以我在用c#编程保龄球应用程序以计算5个不同的分数、将它们存储在数组中并返回平均、最高和最低分数时遇到了问题,我在存储数组和返回分数的代码方面遇到了问题。以下是我目前所拥有的:

static void Main(string[] args)
{
  //Declarations
  const double MIN_SCORE = 0;
  const double MAX_SCORE = 300;
  const int SCORE_COUNT = 5;
  int[] scores = new int[SCORE_COUNT];  //stores all the scores
  int inputScore;                          //stores one score
  double total = 0;       //to total the scores for average
  double average;     //average the grades
  double highScore;   //highest score of the games
  double lowScore;    //lowest score of the games
  //INPUT
  //loop to get scores
  for  (int bowler = 0; bowler < scores.Length; bowler++)
  {
    try
    {
      //prompt for and get the input
      Console.Write("Please enter score for game " + (bowler + 1) + ":");
      inputScore = Convert.ToInt16(Console.ReadLine());
      //valid range?
      if (inputScore > MAX_SCORE || inputScore < MIN_SCORE)
      {
        Console.WriteLine("Scores must be between " + MIN_SCORE + 
              " and " + MAX_SCORE + ". Please try again. ");
        bowler--;
      }
      else
      {
        scores[bowler] = inputScore;
      }
    }
    catch (Exception myEx)
    {
      Console.WriteLine(myEx.Message + " Please try again. ");
      bowler--;             
    }
    //PROCESS
    Array.Sort(scores); 
    //OUTPUT 
    Console.WriteLine("'n'nAverage Score for Bowler:{0}");
  }
}

C#保龄球阵列程序

添加此using语句:

using System.Linq;

然后你可以使用:

scores.Average();
scores.Max();
scores.Min();

很简单。