【发布时间】:2022-01-09 06:00:20
【问题描述】:
我没有找到任何东西可以回答这个问题,而且我认为这与我的代码不同。可能是因为我将自定义对象添加到数组中,但我仍然不知道是什么原因造成的。错误在第 35 行。代码附在下面。
using System;
class Player {
public string abilitySet;
public string name;
public void useAbility() {
if (abilitySet == "fire") {
Console.WriteLine(name + " used fireball which did 40 damage!");
}
}
}
class Match {
public int PlayerCount;
public Player[] players;
public void Start() {
Console.WriteLine("Game started! There are " + PlayerCount + " players!");
}
}
class Program {
public static void Main(string[] args) {
Match match = new Match();
Console.WriteLine("How many players are there?");
string playerCount = Console.ReadLine();
if (Int32.TryParse(playerCount, out int j)) {
Console.WriteLine("You have selected " + playerCount + " players!");
match.PlayerCount = j;
for (int i = 0; i < j; i++) {
Player plr = new Player();
match.players[i] = plr;
plr.name = "Player " + i;
Console.WriteLine("What do you want " + plr.name + "'s ability set to be?");
string ability = Console.ReadLine();
if (ability.ToLower() == "fire") {
Console.WriteLine(plr.name + " has " + ability + "!");
} else {
Console.WriteLine("That is not a ability!");
}
}
} else {
Console.WriteLine("Please enter a number of players not text!");
}
}
}
【问题讨论】:
-
错误是什么?有问题的行是否如下:
plr.name = "Player " + i;? -
感谢您告诉我们行号。但是,我不会数数 35 行代码才能找到它。以后在第 35 行末尾添加注释 lie
//error here, line 35