所以我遇到了在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}");
}
}
答案 0 :(得分:1)
添加此using
声明:
using System.Linq;
然后你可以使用:
scores.Average();
scores.Max();
scores.Min();
足够简单。