C# 使用数组拆分方法分隔名称/数字
我被指示编写一个程序,从用户那里收集保龄球分数(整数)和名称,用空格分隔,并使用C# 使用数组拆分方法分隔名称/数字,c#,arrays,C#,Arrays,我被指示编写一个程序,从用户那里收集保龄球分数(整数)和名称,用空格分隔,并使用Split方法将它们排序到一个数组中。必须对输出进行格式化,以找到平均分数并将其输出到控制台,并按从低到高的顺序对分数(和名称)进行排序 我已经能够做任何事情,除了找到一种方法来排序的名称与相应的分数 这是我到目前为止所拥有的。为了澄清,我需要帮助为我的名字数组编写排序算法 using System; class Program { //class variables const int MAX =
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
使用系统;
班级计划
{
//类变量
常数int MAX=10;
静态void Main()
{
//声明数组
int[]分数=新int[MAX];
字符串[]名称=新字符串[MAX];
//节目开场白
Console.WriteLine(“**********************保龄球项目*************************”);
Console.WriteLine(“请输入每个玩家的姓名和分数。例如“John 145”。\n完成后请输入。”);
//for循环获取输入
对于(int i=0;i得分[i+1])
{
交换(参考分数[i],参考分数[i+1]);
}
}
}
foreach(分数中的整数a)
Console.WriteLine(“{0}”,a);
Console.ReadLine();
}
}//期末课程
您应该创建一个单独的类来存储一个玩家及其分数数组
然后制作一个玩家数组,您可以根据玩家的姓名对其进行排序
编辑:我已经将我的答案更新为一个基于原始代码的实现的播放器类。如果这是一个大学项目,这可能会被剽窃检查人员看到,所以我会小心使用它
public class Player
{
public Player(){}
public Player(string name, int score)
{
m_name = name;
m_score = score;
}
public Player(Player p)
{
m_name = p.Name;
m_score = p.Score;
}
public string Name
{
get{return m_name;}
}
public int Score
{
get{return m_score;}
}
private string m_name
private int m_score
}
Player[] players = new Player[MAX];
static void BubbleSort(Player[] players)
{
for (int j = 0; j < players.Length -1; j++)
{
for (int i = 0; i < players.Length - 1; i++)
{
if (players[i].Score > players[i + 1].Score)
{
Swap(ref players[i], ref players[i + 1]);
}
}
}
foreach (Player a in players)
Console.WriteLine("{0}", a.Score);
Console.ReadLine();
}
static void Swap(ref Player a, ref Player b)
{
Player temp = a;
a = b;
b = temp;
}
公共类播放器
{
公共播放器(){}
公共播放器(字符串名称、整数分数)
{
m_name=名称;
m_分数=分数;
}
公共玩家(玩家p)
{
m_name=p.name;
m_分数=p分数;
}
公共字符串名
{
获取{return m_name;}
}
公共整数分数
{
获取{return m_score;}
}
私有字符串m_名称
私人智力测验分数
}
玩家[]玩家=新玩家[MAX];
静态void BubbleSort(玩家[]玩家)
{
对于(int j=0;j玩家[i+1]。得分)
{
交换(参考球员[i],参考球员[i+1]);
}
}
}
foreach(玩家中的玩家a)
Console.WriteLine(“{0}”,a.Score);
Console.ReadLine();
}
静态无效交换(参考球员a、参考球员b)
{
玩家温度=a;
a=b;
b=温度;
}
您应该创建一个单独的类来存储一个玩家及其分数数组
然后制作一个玩家数组,您可以根据玩家的姓名对其进行排序
编辑:我已经将我的答案更新为一个基于原始代码的实现的播放器类。如果这是一个大学项目,这可能会被剽窃检查人员看到,所以我会小心使用它
public class Player
{
public Player(){}
public Player(string name, int score)
{
m_name = name;
m_score = score;
}
public Player(Player p)
{
m_name = p.Name;
m_score = p.Score;
}
public string Name
{
get{return m_name;}
}
public int Score
{
get{return m_score;}
}
private string m_name
private int m_score
}
Player[] players = new Player[MAX];
static void BubbleSort(Player[] players)
{
for (int j = 0; j < players.Length -1; j++)
{
for (int i = 0; i < players.Length - 1; i++)
{
if (players[i].Score > players[i + 1].Score)
{
Swap(ref players[i], ref players[i + 1]);
}
}
}
foreach (Player a in players)
Console.WriteLine("{0}", a.Score);
Console.ReadLine();
}
static void Swap(ref Player a, ref Player b)
{
Player temp = a;
a = b;
b = temp;
}
公共类播放器
{
公共播放器(){}
公共播放器(字符串名称、整数分数)
{
m_name=名称;
m_分数=分数;
}
公共玩家(玩家p)
{
m_name=p.name;
m_分数=p分数;
}
公共字符串名
{
获取{return m_name;}
}
公共整数分数
{
获取{return m_score;}
}
私有字符串m_名称
私人智力测验分数
}
玩家[]玩家=新玩家[MAX];
静态void BubbleSort(玩家[]玩家)
{
对于(int j=0;j玩家[i+1]。得分)
{
交换(参考球员[i],参考球员[i+1]);
}
}
}
foreach(玩家中的玩家a)
Console.WriteLine(“{0}”,a.Score);
Console.ReadLine();
}
静态无效交换(参考球员a、参考球员b)
{
玩家温度=a;
a=b;
b=温度;
}
假设名称[]
对应1对1的分数[]
:
只需使用您的BubbleSort
方法,将names[]
和score[]
传递到该方法中即可
然后,每当您在score[]
上执行操作时,您也可以在names[]
上执行该操作
像这样的
static void BubbleSort(int[] score, string[] names)
{
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]);
Swap(ref names[i], ref names[i + 1]);
}
}
}
foreach (int a in score)
Console.WriteLine("{0}", a);
Console.ReadLine();
}
假设names[]
对应于1和score[]
:
只需使用您的BubbleSort
方法,将names[]
和score[]
传递到该方法中即可
然后,每当您在score[]
上执行操作时,您也可以在names[]
上执行该操作
像这样的
static void BubbleSort(int[] score, string[] names)
{
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]);
Swap(ref names[i], ref names[i + 1]);
}
}
}
foreach (int a in score)
Console.WriteLine("{0}", a);
Console.ReadLine();
}
不要使用两个松散关联的值集合,而是使用一个类集合:
public class BowlingScore {
public string Name { get; private set; }
public int Score { get; private set; }
public BowlingScore(string name, int score) {
Name = name;
Score = score;
}
}
对集合使用列表而不是数组:
List<BowlingScore> scores = new List<BowlingScore>();
通过使用列表而不是数组,它将只包含您实际添加的项。您当前的代码将需要另一个变量来跟踪数组中实际使用了多少项,这样您就不会在最后包含保留为空的项
现在,由于将名称和分数作为单个单元,因此可以使用与排序数组相同的方法对列表进行排序,只需比较对象的属性。交换整个对象时,在对分数排序时,名称将跟随分数,反之亦然
列表中的项目可以像数组一样使用索引进行访问,只有列表的长度在Count
属性中,而不是length
使用类的单个集合,而不是使用两个松散关联的值集合:
public class BowlingScore {
public string Name { get; private set; }
public int Score { get; private set; }
public BowlingScore(string name, int score) {
Name = name;
Score = score;
}
}
对集合使用列表而不是数组:
List<BowlingScore> scores = new List<BowlingScore>();
通过使用l