String.Format() 问题 - FormatException
Issue with String.Format() - FormatException
我正在尝试制作一个简单的霰弹枪游戏,其中用户与 CPU 以及两者都选择射击、盾牌或重新加载但它显示的结果我得到错误:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Index (zero based) must be greater than or
equal to zero and less than the size of the argument list.
//Declare Variables
int CPUBullets = 3, userBullets = 3;
ShotgunOption UserOption;
ShotgunOption CPUOption;
int userScore = 0;
int CPUscore = 0;
double gameCount = 0.0;
Random computer = new Random();
Console.Clear();
Console.WriteLine("Welcome To The Shotgun Game");
Console.WriteLine("SHOOT RELOAD SHIELD");
do
{
// Prompt User/ Get Random Value From CPU
//Console.Write("Best of 5, Please enter choice: ");
UserOption = GetOptionFromUser();
CPUOption = (ShotgunOption)computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield
switch (UserOption)
{
case ShotgunOption.Shoot:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", UserOption);
; userBullets--; CPUBullets--;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption);
++userScore; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
case ShotgunOption.Reload:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", UserOption);
++gameCount; ++CPUscore;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", UserOption);
userBullets++; CPUBullets++; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
case ShotgunOption.Shield:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption);
++userScore; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
}
} while (gameCount < 4);
这是发生错误的 DisplayResults()
方法:
static void DisplayResults(ShotgunOption UserOption, ShotgunOption CPUOption, int UserScore, int UserBullets, int CPUBullets)
{
Console.Clear();
Console.WriteLine("Round Over");
Console.WriteLine("You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s).", UserOption, CPUOption, UserScore, UserBullets, CPUBullets);
Console.WriteLine("Thanks for playing!");
Console.ReadKey();
}
您的格式字符串缺少索引(具体来说,位置 2):
"You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s)."
^^^
应该是:
"You Chose {0}, The Computer Chose{1} Your Score is {2} . You had {3} Bullet(s). The CPU had {4} bullets(s)."
我正在尝试制作一个简单的霰弹枪游戏,其中用户与 CPU 以及两者都选择射击、盾牌或重新加载但它显示的结果我得到错误:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
//Declare Variables
int CPUBullets = 3, userBullets = 3;
ShotgunOption UserOption;
ShotgunOption CPUOption;
int userScore = 0;
int CPUscore = 0;
double gameCount = 0.0;
Random computer = new Random();
Console.Clear();
Console.WriteLine("Welcome To The Shotgun Game");
Console.WriteLine("SHOOT RELOAD SHIELD");
do
{
// Prompt User/ Get Random Value From CPU
//Console.Write("Best of 5, Please enter choice: ");
UserOption = GetOptionFromUser();
CPUOption = (ShotgunOption)computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield
switch (UserOption)
{
case ShotgunOption.Shoot:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", UserOption);
; userBullets--; CPUBullets--;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption);
++userScore; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
case ShotgunOption.Reload:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", UserOption);
++gameCount; ++CPUscore;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", UserOption);
userBullets++; CPUBullets++; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
case ShotgunOption.Shield:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption);
++userScore; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
}
} while (gameCount < 4);
这是发生错误的 DisplayResults()
方法:
static void DisplayResults(ShotgunOption UserOption, ShotgunOption CPUOption, int UserScore, int UserBullets, int CPUBullets)
{
Console.Clear();
Console.WriteLine("Round Over");
Console.WriteLine("You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s).", UserOption, CPUOption, UserScore, UserBullets, CPUBullets);
Console.WriteLine("Thanks for playing!");
Console.ReadKey();
}
您的格式字符串缺少索引(具体来说,位置 2):
"You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s)."
^^^
应该是:
"You Chose {0}, The Computer Chose{1} Your Score is {2} . You had {3} Bullet(s). The CPU had {4} bullets(s)."