0
我需要找到一個客戶來排序名稱和分數從最高到最低。我不確定如何使用交換方法,並且如果有人能幫助讓我看到方式,我會很喜歡它。另外,有沒有辦法顯示分數,讓它們排列整齊一些?如何使用交換方法將數組從高到低排序?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
class Program
{
static void Main(string[] args)
{
// declare and array of integers
string[] name = new string[5];
int[] score = new int[5];
Console.WriteLine("\nSaturday Coder's Bowling Team");
Console.WriteLine("Enter in a name and score for each person on the team.");
Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.\n");
// fill an array with user input
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine("Enter in a name and score: ");
//save the name and score as a string
string line = Console.ReadLine();
//split the name and score
name[i] = line.Substring(0, line.IndexOf(' '));
score[i] = int.Parse(line.Substring(line.IndexOf(' ') + 1));
}
Console.WriteLine("------------ Input Complete ------------\n");
Console.WriteLine("Here are the scores for this game, from highest to lowest:\n");
for (int i = 0; i < score.Length; i++)
{
if (score[i] >= 300)
{
Console.WriteLine("{0} {1}*.", name[i], score[i]);
}
else
{
Console.WriteLine("{0} {1}", name[i], score[i]);
}
}
//display the average score in the program
AverageScore(score);
Console.WriteLine("Press Enter to continue. . .");
//end program
Console.ReadLine();
}
// find the average score from the score array
static void AverageScore(int[] score)
{
int sum = score.Sum();
int average = sum/score.Length;
Console.WriteLine("\nThe average score for this game was {0:D}.\n", average);
}
}
}
需要實現一種方法將測試對方的分數條目,一旦你發現某些東西是更大或更小的交換這些元素與另一種方法... – Ubica 2014-12-08 02:15:00