String.Format()的问题 - FormatException

时间:2015-06-25 03:47:11

标签: c# string.format formatexception

我正在尝试制作一个简单的霰弹枪游戏,其中用户与CPU以及拾取镜头,屏蔽或重新加载,但它显示结果我得到错误:

  

未处理的类型' System.FormatException'发生在   mscorlib.dll中

     

附加信息:索引(从零开始)必须大于或   等于零且小于参数列表的大小。

//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();

}

1 个答案:

答案 0 :(得分:0)

您的格式字符串缺少索引(具体而言,位置 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)."