【问题标题】:How to use bool operator in如何使用布尔运算符
【发布时间】:2018-10-15 15:38:20
【问题描述】:

我的主要问题是,如何将索引发送给我的布尔运算符? 我尝试在我的PlayerContainer 课程中使用int = 0; 然后this player.GetPlayer(i++).KA,但i = 0 一直都是。
player.GetPlayer(i).KA 是玩家的杀戮+助攻,如果这样更容易理解的话。

这段代码是PlayerContainer.cs类。

class PlayerContainer {
 public Player[] Players;
 public int Count {
  get;
  set;
 }
 public int cycle {
  get;
  set;
 }
 public DateTime date {
  get;
  set;
 }


 public PlayerContainer(int size) {
  Players = new Player[size];
 }
 public void AddPlayer(Player player) {
  Players[Count++] = player;
 }
 public void AddPlayer(Player player, int index) {
  Players[index] = player;
 }
 public Player GetPlayer(int index) {
  return Players[index];
 }
 public static bool operator < (int max, PlayerContainer player) {
  if (max < player.GetPlayer(i++).KA) {
   return true;
  } else
   return false;
 }
 public static bool operator > (int max, PlayerContainer player) {
  int i = 0;
  if (max < player.GetPlayer(i++).KA)
   return true;
  else
   return false;
 }

这是在我的 Program.cs 类中的方法 BestPlayer 中

Player BestPlayer(PlayerContainer AllPlayers) {
 Player player = AllPlayers.GetPlayer(0);
 int max = AllPlayers.GetPlayer(0).KA;
 for (int i = 0; i < AllPlayers.Count; i++) {
  if (max < AllPlayers) {
   max = AllPlayers.GetPlayer(i).KA;
   player = AllPlayers.GetPlayer(i);
  }
 }
 return player;
}

【问题讨论】:

    标签: c# operators


    【解决方案1】:
    Player BestPlayer(PlayerContainer AllPlayers)
    {
        Player player;
        int max = AllPlayers.GetPlayer(0).KA;
        Player best = AllPlayers.GetPlayer(0);
        int tmp;
        for (int i = 1; i < AllPlayers.Count; i++)
        {
            tmp = AllPlayers.GetPlayer(i).KA;
            player = AllPlayers.GetPlayer(i);
            if (tmp > AllPlayers)
            {
                 max = tmp;
                 best = player;
            }
        }
        return best;
      }
    

    【讨论】:

    • 本地,您的意思是我必须在该运算符之前或在哪里声明它?如果我在运算符中定义int i = 0,它总是一样的。
    猜你喜欢
    • 2011-04-20
    • 2011-09-27
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多