继承-存储对象列表<;类别>;带循环C#
我被困在一个练习中,这个练习对于我即将到来的期末考试非常重要,这是一门基础C#课程 我有一个无法用语言描述的问题。因此,我将向您展示代码,或许您可以帮助我。 我一直被困在这个问题上,很长一段时间都无法解决它。因此,我不想要任何复制粘贴代码。我想了解它。所以请告诉我哪里失败了 解释我想做什么。 我想创建一个飞镖游戏501。首先我添加玩家,他们投掷飞镖,你在宣布每回合后得到分数,然后当一个玩家达到501分时,游戏会宣布胜利者和他的所有投掷 我解决这个问题的想法是有一个addplayer循环,该循环终止(我已经修复了这个问题)。 创建完玩家(列表元素)后,您将使用foreach循环执行这些方法,该循环运行玩家列表中的所有玩家,一次执行一个对象,最后我真正的问题是:将他们的所有分数存储在另一个列表中 我们来看看代码继承-存储对象列表<;类别>;带循环C#,c#,list,constructor,multiple-inheritance,C#,List,Constructor,Multiple Inheritance,我被困在一个练习中,这个练习对于我即将到来的期末考试非常重要,这是一门基础C#课程 我有一个无法用语言描述的问题。因此,我将向您展示代码,或许您可以帮助我。 我一直被困在这个问题上,很长一段时间都无法解决它。因此,我不想要任何复制粘贴代码。我想了解它。所以请告诉我哪里失败了 解释我想做什么。 我想创建一个飞镖游戏501。首先我添加玩家,他们投掷飞镖,你在宣布每回合后得到分数,然后当一个玩家达到501分时,游戏会宣布胜利者和他的所有投掷 我解决这个问题的想法是有一个addplayer循环,该循环终
public void AddDarts(Darts toDartList)
{
dartList.Add(toDartList);
}
名单
子类1(命名玩家)
第2子类(省道)
这里是我的构造函数
private int dartOne;
private int dartOne;
private int dartOne;
这是我的方法
public Darts(int DartOne, int DartTwo, int DartThree)
{
dartOne = DartOne;
dartTwo = DartTwo;
dartThree = DartThree;
}
向马库斯·约翰逊致意
这是我的完整课程
class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.PlayGame();
}
}
class Game
{
private List<Player> players = new List<Player>();
private List<Player> computers = new List<Player>();
public void AddPlayer(string newPlayers)
{
players.Add(new Player(newPlayers));
}
public void AddComputer(string newComputer)
{
computers.Add(new Player(newComputer));
}
static string UpperCaseFirst(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
return char.ToUpper(s[0]) + s.Substring(1);
}
public void PlayGame()
{
bool noWinner = false;
bool stopLoop = false;
Console.WriteLine("<<<WELCOME TO DART 501>>>");
Console.WriteLine("\nPress [S] to start game!");
Console.Beep();
Console.ReadLine();
Console.Clear();
do
{
Console.WriteLine("Enter name of players and type [D]ator for adding NPC\nType [S]top to start the game");
string addPlayer = Console.ReadLine();
string FirstUpperLetter = UpperCaseFirst(addPlayer);
if (FirstUpperLetter == "Stop" || FirstUpperLetter == "S")
{
stopLoop = true;
}
if (FirstUpperLetter == "D" || FirstUpperLetter == "Dator")
{
string computer = FirstUpperLetter;
AddComputer(computer);
}
else
{
AddPlayer(FirstUpperLetter);
}
} while (stopLoop == false) ;
players.RemoveAt(players.Count - 1);
do
{
Console.Clear();
foreach (Player arrowThrows in players)
{
noWinner = true;
Console.WriteLine("\n~~~Starting Round~~~~");
arrowThrows.DoThrow();
Console.WriteLine("This round you got {0}", arrowThrows.CalculatePoints());
if (arrowThrows.Score > 501)
{
Console.Clear();
Console.WriteLine("<<<WE HAVE A WINNER>>>");
System.Threading.Thread.Sleep(500);
Console.WriteLine("...The winner is: ");
System.Threading.Thread.Sleep(1000);
Console.WriteLine("{0} He made these epic throws: ", arrowThrows.Name);
foreach(Arrows finalResult in arrowThrows.arrowList)
{
Console.WriteLine(finalResult);
Console.ReadLine();
}
noWinner = false;
}
}
foreach (Player computerThrows in computers)
{
computerThrows.RandomThrow();
System.Threading.Thread.Sleep(500);
}
}while(noWinner == true);
}
}
class Player
{
public List<Arrows> arrowList = new List<Arrows>();
public List<int> ScoreBoard = new List<int>();
public Player() { }
public int Score { get; set; }
public Player(int score)
{
Score = score;
}
public string Name { get; set; }
public Player(string name)
{
Name = name;
}
public int RoundScore { get; set; }
public void RandomThrow()
{
Random rndComp = new Random();
Console.WriteLine("...Loading Npc_throw.exe");
System.Threading.Thread.Sleep(300);
int random1 = rndComp.Next(0, 60);
System.Threading.Thread.Sleep(300);
int random2 = rndComp.Next(0, 60);
System.Threading.Thread.Sleep(300);
int random3 = rndComp.Next(0, 60);
Console.WriteLine("Random computer got this random score {0}", random1 + random2 + random3);
arrowList.Add(new Arrows(random1, random2, random3));
}
public void DoThrow()
{
Console.WriteLine("###{0} make your throws###", Name);
var tries = 3;
for (int i = 0; i < tries; i++)
{
Console.WriteLine("\nEnter score for {0} arrow", i + 1);
string arrowScore = Console.ReadLine();
int addScore = int.Parse(arrowScore);
while(-1 > addScore || 61 < addScore)
{
Console.WriteLine("\nInvalid score! Enter a score between 0-60/n<<<You may type [R]andom or [R] for a random score>>>");
arrowScore = Console.ReadLine().ToUpper();
if (arrowScore == "R" || arrowScore == "RANDOM")
{
Random rnd = new Random();
addScore = rnd.Next(0, 60);
goto start;
}
else
{
addScore = int.Parse(arrowScore);
}
}
start:
ScoreBoard.Add(addScore);
}
ScoreBoard.ToArray();
arrowList.Add(new Arrows(ScoreBoard[0],ScoreBoard[1], ScoreBoard[2]));
}
public int CalculatePoints()
{
Score = ScoreBoard.Sum();
return Score;
}
public void AddArrows(Arrows toArrowList)
{
toArrowList.ToString();
arrowList.Add(new Arrows(ScoreBoard[0], ScoreBoard[1], ScoreBoard[2]));
}
}
class Arrows
{
private int arrowOne;
private int arrowTwo;
private int arrowThree;
public int score { get; set; }
public Arrows(int ArrowOne, int ArrowTwo, int ArrowThree)
{
arrowOne = ArrowOne;
arrowTwo = ArrowTwo;
arrowThree = ArrowThree;
}
public int GetScore()
{
return arrowOne + arrowTwo + arrowThree;
}
public override string ToString()
{
return string.Format("{0}-1:st arrow: {1}-2:nd arrow: {2}- 3:rd arrow: {3}", GetScore(), arrowOne, arrowTwo, arrowThree);
}
}
类程序
{
静态void Main(字符串[]参数)
{
游戏=新游戏();
游戏;
}
}
班级游戏
{
私人名单玩家=新名单();
私有列表计算机=新列表();
public void AddPlayer(字符串newPlayers)
{
players.Add(新玩家(newPlayers));
}
公用计算机(字符串新计算机)
{
添加(新玩家(新电脑));
}
静态字符串大写字母优先(字符串s)
{
if(string.IsNullOrEmpty)
{
返回字符串。空;
}
返回字符ToUpper(s[0])+s子字符串(1);
}
公共游戏
{
bool nowniner=false;
bool-stopLoop=false;
控制台。写线(“”);
Console.WriteLine(“\n按[S]开始游戏!”);
Console.Beep();
Console.ReadLine();
Console.Clear();
做
{
Console.WriteLine(“输入玩家名称并键入[D]ator以添加NPC\n键入[S]top以开始游戏”);
字符串addPlayer=Console.ReadLine();
字符串FirstUpperLetter=大写字母first(addPlayer);
if(FirstUpperLetter==“Stop”| | FirstUpperLetter==“S”)
{
stopLoop=true;
}
if(FirstUpperLetter==“D”| | FirstUpperLetter==“Dator”)
{
字符串计算机=第一个大写字母;
AddComputer(计算机);
}
其他的
{
AddPlayer(第一个大写字母);
}
}while(stopLoop==false);
players.RemoveAt(players.Count-1);
做
{
Console.Clear();
foreach(玩家箭头插入玩家)
{
nowniner=true;
Console.WriteLine(“\n~~~~开始回合~~~”;
arrow.DoThrow();
WriteLine(“这一轮你得到了{0}”,arrowThrows.CalculatePoints());
如果(arrow.Score>501)
{
Console.Clear();
控制台。写线(“”);
系统.线程.线程.睡眠(500);
Console.WriteLine(“…获胜者是:”);
系统线程线程睡眠(1000);
WriteLine({0}他进行了这些史诗般的抛出:“,arrowthows.Name);
foreach(Arrows-finalResult在arrow-throws.arrow列表中)
{
控制台写入线(最终结果);
Console.ReadLine();
}
nowniner=false;
}
}
foreach(玩家电脑输入电脑)
{
computerThrows.RandomThrow();
系统.线程.线程.睡眠(500);
}
}while(nowniner==true);
}
}
职业选手
{
公共列表箭头列表=新列表();
公共列表记分板=新列表();
公共播放器(){}
公共整数分数{get;set;}
公共玩家(智力分数)
{
分数=分数;
}
公共字符串名称{get;set;}
公共播放器(字符串名称)
{
名称=名称;
}
公共整数舍入得分{get;set;}
公共图书馆
{
Random rndComp=新的Random();
WriteLine(“…加载Npc_throw.exe”);
系统.线程.线程.睡眠(300);
int random1=rndComp.Next(0,60);
系统.线程.线程.睡眠(300);
int random2=rndComp.Next(0,60);
系统.线程.线程.睡眠(300);
int random3=rndComp.Next(0,60);
WriteLine(“随机计算机得到这个随机分数{0}”,random1+random2+random3);
添加(新箭头(随机1、随机2、随机3));
}
公共无效DoThrow()
{
Console.WriteLine(“###{0}进行抛出####”,Name);
var=3;
for(int i=0;iaddScore | | 61 public void AddDarts(Darts toDartList)
{
dartList.Add(toDartList);
}
private int dartOne;
private int dartOne;
private int dartOne;
public Darts(int DartOne, int DartTwo, int DartThree)
{
dartOne = DartOne;
dartTwo = DartTwo;
dartThree = DartThree;
}
class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.PlayGame();
}
}
class Game
{
private List<Player> players = new List<Player>();
private List<Player> computers = new List<Player>();
public void AddPlayer(string newPlayers)
{
players.Add(new Player(newPlayers));
}
public void AddComputer(string newComputer)
{
computers.Add(new Player(newComputer));
}
static string UpperCaseFirst(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
return char.ToUpper(s[0]) + s.Substring(1);
}
public void PlayGame()
{
bool noWinner = false;
bool stopLoop = false;
Console.WriteLine("<<<WELCOME TO DART 501>>>");
Console.WriteLine("\nPress [S] to start game!");
Console.Beep();
Console.ReadLine();
Console.Clear();
do
{
Console.WriteLine("Enter name of players and type [D]ator for adding NPC\nType [S]top to start the game");
string addPlayer = Console.ReadLine();
string FirstUpperLetter = UpperCaseFirst(addPlayer);
if (FirstUpperLetter == "Stop" || FirstUpperLetter == "S")
{
stopLoop = true;
}
if (FirstUpperLetter == "D" || FirstUpperLetter == "Dator")
{
string computer = FirstUpperLetter;
AddComputer(computer);
}
else
{
AddPlayer(FirstUpperLetter);
}
} while (stopLoop == false) ;
players.RemoveAt(players.Count - 1);
do
{
Console.Clear();
foreach (Player arrowThrows in players)
{
noWinner = true;
Console.WriteLine("\n~~~Starting Round~~~~");
arrowThrows.DoThrow();
Console.WriteLine("This round you got {0}", arrowThrows.CalculatePoints());
if (arrowThrows.Score > 501)
{
Console.Clear();
Console.WriteLine("<<<WE HAVE A WINNER>>>");
System.Threading.Thread.Sleep(500);
Console.WriteLine("...The winner is: ");
System.Threading.Thread.Sleep(1000);
Console.WriteLine("{0} He made these epic throws: ", arrowThrows.Name);
foreach(Arrows finalResult in arrowThrows.arrowList)
{
Console.WriteLine(finalResult);
Console.ReadLine();
}
noWinner = false;
}
}
foreach (Player computerThrows in computers)
{
computerThrows.RandomThrow();
System.Threading.Thread.Sleep(500);
}
}while(noWinner == true);
}
}
class Player
{
public List<Arrows> arrowList = new List<Arrows>();
public List<int> ScoreBoard = new List<int>();
public Player() { }
public int Score { get; set; }
public Player(int score)
{
Score = score;
}
public string Name { get; set; }
public Player(string name)
{
Name = name;
}
public int RoundScore { get; set; }
public void RandomThrow()
{
Random rndComp = new Random();
Console.WriteLine("...Loading Npc_throw.exe");
System.Threading.Thread.Sleep(300);
int random1 = rndComp.Next(0, 60);
System.Threading.Thread.Sleep(300);
int random2 = rndComp.Next(0, 60);
System.Threading.Thread.Sleep(300);
int random3 = rndComp.Next(0, 60);
Console.WriteLine("Random computer got this random score {0}", random1 + random2 + random3);
arrowList.Add(new Arrows(random1, random2, random3));
}
public void DoThrow()
{
Console.WriteLine("###{0} make your throws###", Name);
var tries = 3;
for (int i = 0; i < tries; i++)
{
Console.WriteLine("\nEnter score for {0} arrow", i + 1);
string arrowScore = Console.ReadLine();
int addScore = int.Parse(arrowScore);
while(-1 > addScore || 61 < addScore)
{
Console.WriteLine("\nInvalid score! Enter a score between 0-60/n<<<You may type [R]andom or [R] for a random score>>>");
arrowScore = Console.ReadLine().ToUpper();
if (arrowScore == "R" || arrowScore == "RANDOM")
{
Random rnd = new Random();
addScore = rnd.Next(0, 60);
goto start;
}
else
{
addScore = int.Parse(arrowScore);
}
}
start:
ScoreBoard.Add(addScore);
}
ScoreBoard.ToArray();
arrowList.Add(new Arrows(ScoreBoard[0],ScoreBoard[1], ScoreBoard[2]));
}
public int CalculatePoints()
{
Score = ScoreBoard.Sum();
return Score;
}
public void AddArrows(Arrows toArrowList)
{
toArrowList.ToString();
arrowList.Add(new Arrows(ScoreBoard[0], ScoreBoard[1], ScoreBoard[2]));
}
}
class Arrows
{
private int arrowOne;
private int arrowTwo;
private int arrowThree;
public int score { get; set; }
public Arrows(int ArrowOne, int ArrowTwo, int ArrowThree)
{
arrowOne = ArrowOne;
arrowTwo = ArrowTwo;
arrowThree = ArrowThree;
}
public int GetScore()
{
return arrowOne + arrowTwo + arrowThree;
}
public override string ToString()
{
return string.Format("{0}-1:st arrow: {1}-2:nd arrow: {2}- 3:rd arrow: {3}", GetScore(), arrowOne, arrowTwo, arrowThree);
}
}
public class Player
{
public string Name { get; set; }
public string Throw { get; set; }
public string Points { get; set; }
}
Dictionary<Player, List<int>> playerScores = new Dictionary<Player, List<int>>();
playerScores.Add(player1, new List<int>()); //init dictionary with a new player
playerScores[player1].Add(number); //add a value to the list associated with the player