程序在运行过程中结束


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 younPress any key to exit");
}
else if (words == 1) {
Console.WriteLine("The enemy has slaughtered younPress any key to exit");
}
else if (words == 2) {
Console.WriteLine("The enemy has murdered younPress any key to exit");
}
else if (words == 3) {
Console.WriteLine("The enemy has defeated younPress any key to exit");
}
else if (words == 4) {
Console.WriteLine("The enemy has beheaded younPress any key to exit");
}
else if (words == 5) {
Console.WriteLine("The enemy has impaled younPress any key to exit");
}
else {
Console.WriteLine("The enemy has shredded younPress any key to exit");
}

Console.ReadKey();

if (enemyHealth == 0) {
Random deathSentence2 = new Random();
int words2 = deathSentence2.Next(7);
string[] wordsSaying2 = {"slain","slaughtered","murdered","defeated","beheaded","impaled","shredded"};
if (words2 == 0) {
Console.WriteLine("You have slain the enemynPress any key to exit");
}
else if (words2 == 1) {
Console.WriteLine("You have slaughtered the enemynPress any key to exit");
}
else if (words2 == 2) {
Console.WriteLine("You have murdered the enemynPress any key to exit");
}
else if (words2 == 3) {
Console.WriteLine("You have defeated the enemynPress any key to exit");
}
else if (words2 == 4) {
Console.WriteLine("You have beheaded the enemynPress any key to exit");
}
else if (words2 == 5) {
Console.WriteLine("You have impaled the enemynPress any key to exit");
}
else {
Console.WriteLine("You have shredded the enemynPress any key to exit");
}

Console.ReadKey();
}
}
else {
Console.WriteLine("That is not one of the available limbs, please select one of the following:nHeadnTorsonArmsnLegs");
}                
}
break;
}
Console.ReadKey();
}
}
}
}

我的代码开始了,然后当我输入我想尝试点击的区域时,它移动到第48行,在那里它开始打印出";你试图打击"+"enemyUsing+";在";等但它键入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:nHeadnTorsonArmsnLegs");
}

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

else
{
Console.WriteLine("You have shredded the enemynPress any key to exit");
}
Console.ReadKey();
break; // exit while loop or return; if you want to stop game 

最新更新