如果我试试这个:
int count = int.TryParse(Console.ReadLine(), out count) ? count : default(int);
而不是这个:int count = int.Parse(Console.ReadLine());
问题已解决,但随后出现 Array out of range 错误。我应该怎么办 ?
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
class Player
{
static void Main(String[] args)
{
string[] inputs;
// game loop
while (true)
{
int count = int.Parse(Console.ReadLine()); // The number of current enemy ships within range
Console.Error.WriteLine("Count:" + count);
Enemy[] enemys = new Enemy[count];
for (int i = 0; i < count; i++)
{
inputs = Console.ReadLine().Split(' ');
enemys[i] = new Enemy(inputs[0], int.Parse(inputs[1]));
}
Array.Sort(enemys, delegate(Enemy en1, Enemy en2) {
return en1.Dist.CompareTo(en2.Dist);
});
Console.WriteLine(enemys[0].Name);
}
}
}
public class Enemy{
public string Name;
public int Dist;
public Enemy(string name, int dist){
this.Name = name;
this.Dist = dist;
}
}