如何创建和打印项目列表。 [英] How do I create and print out a list of Items.

查看:87
本文介绍了如何创建和打印项目列表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类Player,我想使用一个创建播放器列表的方法。我写了一些代码,但有些东西是错的,我不知道是什么。如果我能够在控制台打印的列表是最后一个列表,那么唯一的记录。我做错了什么?



以下是我写的代码......



文件播放器.cs



I have created a class Player and I want to use a method that creates a list of Players. I wrote some code but something is wrong and i don't know what. The only record if the list that I am able to print out at the Console is the last one. What am I doing wrong?

Below is the code that i wrote...

File Player.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise10dot6
{
    class Player
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private double balance;
        private bool isActive;
        private string password;

        //public string Password
        //{
        //    get { return password; }
        //    set { password = value; }
        //}

        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }


        public double Balance
        {
            get { return balance; }
            set { balance = value; }
        }

        public Player()
        {
            // A constructor to initialize a new player given a name, balance and password. The player should be initialized as inactive.
            this.name = "Iraklis Markelis";
            this.balance = 0.9;
            this.password = "*******";
            this.isActive = false;
        }

        public List<Player> Players()
        {
            bool finish = false;
            List<Player> ps = new List<Player>();

            while (!finish)
            {
                Console.Write("Type player's name: ");
                this.name = Console.ReadLine();
                Console.Write("Type player's balance: ");
                this.balance = double.Parse(Console.ReadLine());
                Console.Write("Give a password: ");
                this.password = Console.ReadLine();
                this.isActive = false;
                ps.Add(this);
                Console.Write("Do you want to continue? (0/1): ");
                finish = Convert.ToBoolean(Convert.ToInt16(Console.ReadLine()));
            }
            return ps;
        }

        public string PlayerInfo()
        {
            // A method to return a string that shows the public fields of a player separated by tabs.
            return "Name: " + this.name + "\tBalance: " + this.balance.ToString() + "\tIs active: " + this.isActive.ToString();
        }

        public bool PasswordAccepted(string password)
        {
            // A method to check a string for being a valid password, returning a bool.
            if (this.password == password)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void ShowPlayers(List<Player> players)
        {
            foreach (Player player in players)
            {
                Console.WriteLine(player.PlayerInfo());
            }
        }

        public Player FindPlayer(Player[] players, string nameToSearch)
        {
            foreach (Player player in players)
            {
                if (nameToSearch == player.name)
                {
                    return player;
                }
            }
            return null;
        }

        public string Login(string name, string password, Player[] players, out Player p)
        {
            p = FindPlayer(players, name);
            if (p == null)
            {
                return "Player not found.";
            }
            else
            {
                if (p.isActive == true)
                {
                    return "Player is active.";
                }
                else if (p.password == password)
                {
                    p.isActive = true;
                    return "OK";
                }
                else
                {
                    return "Wrong password.";
                }
            }
        }

        public string Logout(Player[] players, string name)
        {
            foreach (Player player in players)
            {
                if (player.name == name)
                {
                    if(player.isActive == true)
                    {
                        return "OK";
                    }
                    else
                    {
                        return "Logout failure. Player not logged in";
                    }
                }
                else
                {
                    return "Logout failure. Player not found.";
                }
            }

            return null;
        }
    }
}





File Program.cs





File Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise10dot6
{
    class Program
    {
        static void Main(string[] args)
        {
            Player p = new Player();
            List<Player> myPlayers = new List<Player>();
            myPlayers = p.Players();
            p.ShowPlayers(myPlayers);
            Console.ReadLine();
        }
    }
}





非常感谢你的帮助...



Thank you very much for your help...

推荐答案

我在这里会很残酷,但希望不会让你心烦意乱。然后我会尝试解释你应该做的一些事情! :笑:



这真的很糟糕。它完全错过了面向对象的点,不仅打破了每一条规则,还表明你不明白实例是什么,或者你应该用它做什么。



所以,让我们重新开始吧。



您的播放器类不应该知道如何与用户交谈:这是创建它的代码的工作。因此,将Console代码移出Player,进入Program。无论如何创建一个从用户读取输入的方法 - 但不应该在Player中,因为玩家只知道玩家,而不是用户。



和玩家也不应该知道其他玩家 - 就像四个人在互联网上玩电脑战争游戏时不必在游戏之外相互认识一样,你的玩家类应该关心自己,而不是其他玩家 - 所以尝试使用Player类中的玩家列表是一个坏主意。我建议你创建一个PlayerList类来查看它所知道的所有玩家(这样,你以后可以有两个团队 - 每个团队都知道他们自己的玩家,但不是反对者)



某处,(不在Player内,但可能在PlayerList内)你需要为你想要使用的每个玩家创建一个新的Player实例。



回去,再次阅读你的课程笔记,看看你是否可以适应他们所说的 - 因为你的代码永远不会起作用!

我'从小开始:创建一个玩家并填写他的信息。然后,当它工作时,创建一个球员列表,并添加每个球员。然后......你明白了 - 以小步骤工作,而不是试图将它们全部放在一起然后希望它能够工作! :笑:

任何具体问题,请随时询问 - 但请先阅读您的课程笔记!
I'm going to be brutal here, but hopefully not upset you too much. Then I'll try to explain some of what you should have done! :laugh:

That's really bad. It misses the point of object orientation completely, and not only breaks every rule, it also shows that you don't understand what an instance is, or what you should do with it.

So, let's start again.

Your Player class should not know how to talk to the user: that's the job of the code that creates it. So move the Console code out of Player, and into Program. By all means create a method which reads input from the user - but that shouldn't be in Player, because Player only knows about players, not users.

And Player shouldn't know about other Players either - in the same way that four people playing a computer war game on the internet don't have to know each other outside the game, your Player class should be concerned with itself, not others - so trying to work with the list of players from within the Player class is a bad idea. I'd suggest you create a PlayerList class which looks through all the players it knows about instead (that way, you can later have two teams - each of which know about their own players, but not the opposition)

And somewhere, (not inside Player, but possibly inside PlayerList) you need to create a new Player instance for each Player you want to work with.

Go back, read your course notes again, and see if you can fit any of this into what they say - because the code you have is never going to work!
I'd start small: create a player and fill in his info. Then when that works, create a list of Players, and add each one. Then ... you get the idea - work in small steps instead of trying to throw it all together and then hope it works! :laugh:
Any specific questions, feel free to ask - but read your course notes first!


这篇关于如何创建和打印项目列表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆