我已指示寫收集保齡球得分(整數),並且從用戶的名稱,用空格隔開的程序,以及使用它們中的每排序成陣列Split
方法。輸出必須被格式化找到的平均分數,並將其輸出到控制檯,以及排序成績(和名稱)在最低以最高的。使用數組拆分法分離的名字/號碼
我已經能夠做到,除了找到一個方法來與它們對應的分數排序的名稱一切。
這是我到目前爲止。爲了澄清,我需要幫助爲我的數組名稱編寫排序算法。
using System;
class Program
{
//class variables
const int MAX = 10;
static void Main()
{
//declare array
int[] score = new int[MAX];
string[] name = new string[MAX];
//program prologue
Console.WriteLine("****************Bowling Project****************");
Console.WriteLine("Please enter a name and a score for each player. For example 'John 145'.\nHit enter when you are done.");
//for loop to get input
for (int i=0; i<MAX; i++)
{
Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
string input = Console.ReadLine();
if (input == "")
{
break; // if nothing is entered, it will break the loop
}
//split the user data into 2 arrays (integer and string)
string[] separateInput = input.Split();
name[i] = separateInput[0];
score[i] = int.Parse(separateInput[1]);
}
Console.WriteLine("\n****************Input Complete****************");
//calculate the average score and send to the console
CalculateScores(score);
Console.WriteLine("The scores for the game are:");
//sort the scores
BubbleSort(score);
Console.ReadLine();
}//End Main()
//CalculateScores Method
//Calculates the average score of an array of integers
//Takes an array of integers as parameters
//Returns void
//Outputs the average to the console
static void CalculateScores(int[] score)
{
int sum = 0;
int average = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
average = sum/score.Length;
}
Console.WriteLine("The average score was {0}", average);
}//End calculateScores
//Swap method
//Takes 2 integers as parameters
//Switches the value of 2 integers (a and b)
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
//BubbleSort method
//Sorts an array of integers
//Takes an array of integers as a parameter
//Returns void
//Outputs to the console
static void BubbleSort(int[] score)
{
for (int j = 0; j < score.Length -1; j++)
{
for (int i = 0; i < score.Length - 1; i++)
{
if (score[i] > score[i + 1])
{
Swap(ref score[i], ref score[i + 1]);
}
}
}
foreach (int a in score)
Console.WriteLine("{0}", a);
Console.ReadLine();
}
}//End class Program
請花些時間來正確設置代碼的格式,並且它是作業的事實與被問的問題沒有關係。快速瀏覽一下 – 2013-03-25 16:10:51
:使用2個數組有點奇怪。但無論如何。如果你爲一個數組排序,你爲什麼不交換相同的數組位置給玩家,以便玩家和得分的相應位置是相同的?所以不是({Swap(ref score [i],ref score [i + 1]);})({Swap(ref score [i],ref score [i + 1]); Swap(ref player [i] ,ref player [i + 1]);}) – Offler 2013-03-25 16:15:29
對不起,這是我第一次在這裏問了一年多的問題。我甚至沒有意識到它的格式不正確?哪部分不正確?謝謝 – xavi 2013-03-25 16:16:17