程序在运行中结束

问题描述

using System;
using System.Collections.Generic;

namespace Learning_C__Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int enemyHealth = 1000;
            int playerHealth = 1000;

            string[] limbPicked = {"","",""};

            string[] enemyTypes = {"Ogre","Beast","Dragon"};

            Random enemyPick = new Random();
            int pickEnemy = enemyPick.Next(2);

            // Below is the if statements for when an ogre is picked

            int i = 1;

            while(i == 1) {
                
                if (pickEnemy == 0||pickEnemy == 1||pickEnemy == 2) {

                    string enemyUsing;

                    if (pickEnemy == 0) {
                        enemyUsing = enemyTypes[0];
                    }
                    else if (pickEnemy == 1) {
                        enemyUsing = enemyTypes[1];
                    }
                    else {
                        enemyUsing = enemyTypes[2];
                    }

                    Console.WriteLine("You encounter an unhappy " + enemyUsing + "! It starts a fight!\nSelect a limb to hit: ");
                    limbPicked[0] = Convert.ToString(Console.ReadLine());

                    if (limbPicked[0] == "Head"||limbPicked[0] == "head"||limbPicked[0] == "Torso"||limbPicked[0] == "torso"||limbPicked[0] == "Arms"||limbPicked[0] == "arms"||limbPicked[0] == "Legs"||limbPicked[0] == "legs") {

                    Random hitAmount = new Random();
                    int damage1 = hitAmount.Next(1,300);

                    Console.WriteLine("You attempted to strike the " + enemyUsing + " in the " + limbPicked[0] + " doing " + damage1 + " damage to the " + enemyUsing + "! They Now have " + (enemyHealth - damage1) + " health!");

                    enemyHealth = enemyHealth - damage1;

                    Random enemyHitAmount = new Random();
                    int damage2 = enemyHitAmount.Next(1,300);
                    
                    Console.WriteLine("The enemy attacks! They do " + damage2 + " damage to you! You Now have " + (playerHealth - damage2) + " health!");

                    playerHealth = playerHealth - damage2;

                    if (playerHealth == 0) {

                         Random deathSentence = new Random();
                         int words = deathSentence.Next(7);

                         string[] wordsSaying = {"slain","slaughtered","murdered","defeated","beheaded","impaled","shredded"};

                            if (words == 0) {
                                Console.WriteLine("The enemy has slain you\nPress any key to exit");
                            }
                            else if (words == 1) {
                                Console.WriteLine("The enemy has slaughtered you\nPress any key to exit");
                            }
                            else if (words == 2) {
                                Console.WriteLine("The enemy has murdered you\nPress any key to exit");
                            }
                            else if (words == 3) {
                                Console.WriteLine("The enemy has defeated you\nPress any key to exit");
                            }
                            else if (words == 4) {
                                Console.WriteLine("The enemy has beheaded you\nPress any key to exit");
                            }
                            else if (words == 5) {
                                Console.WriteLine("The enemy has impaled you\nPress any key to exit");
                            }
                            else {
                                Console.WriteLine("The enemy has shredded you\nPress any key to exit");
                            }
                            
                            Console.ReadKey();
                            
                            if (enemyHealth == 0) {

                            Random deathSentence2 = new Random();
                            int words2 = deathSentence2.Next(7);

                            string[] wordsSaying2 = {"slain","shredded"};

                            if (words2 == 0) {
                                Console.WriteLine("You have slain the enemy\nPress any key to exit");
                            }
                            else if (words2 == 1) {
                                Console.WriteLine("You have slaughtered the enemy\nPress any key to exit");
                            }
                            else if (words2 == 2) {
                                Console.WriteLine("You have murdered the enemy\nPress any key to exit");
                            }
                            else if (words2 == 3) {
                                Console.WriteLine("You have defeated the enemy\nPress any key to exit");
                            }
                            else if (words2 == 4) {
                                Console.WriteLine("You have beheaded the enemy\nPress any key to exit");
                            }
                            else if (words2 == 5) {
                                Console.WriteLine("You have impaled the enemy\nPress any key to exit");
                            }
                            else {
                                Console.WriteLine("You have shredded the enemy\nPress any key to exit");
                            }
                            
                            Console.ReadKey();

                        }

                    }
                    else {
                        Console.WriteLine("That is not one of the available limbs,please select one of the following:\nHead\nTorso\nArms\nLegs");
                    }                
                }
             break;

            }

            Console.ReadKey();

        }
    }
}
}

我的代码正在启动,然后当我输入要尝试击中的区域时,它移至第48行,在该行中开始打印出“您试图击中目标中的“ +敌人使用+” “等 但是它键入Y然后由于某种原因而结束!请帮忙!

解决方法

只需在第150行上删除break;,退出while (i == 1)循环

看上去您放错了else块,应该在检查用户输入之后,而不是在if (playerHealth == 0)

之后
else
{
    Console.WriteLine("That is not one of the available limbs,please select one of the following:\nHead\nTorso\nArms\nLegs");
}

break;的位置实际上应该在您的退出条件之一之后

else
{
    Console.WriteLine("You have shredded the enemy\nPress any key to exit");
}

Console.ReadKey();
break; // exit while loop or return; if you want to stop game