我正在做一个基于文本的小游戏,我的一个if / else语句没有给我预期的结果。当用户输掉它时,立即跳转到“Game Over!”应该说monsterName +“击败了你!”然后跳到游戏结束。
你们能帮忙指出我的逻辑错误吗?
// Main Game Loop
while (playerHealth > 0 && monsterHealth > 0)
{
bool playerAct = askAction();
if (playerAct == true)// User attacks
{
monsterHealth = monsterHealth - playerAtk;
if (monsterHealth < 0)
{
Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
}
else
{
Console.WriteLine(monsterName + " takes a mighty swing back at you!");
playerHealth = playerHealth - monsterAtk;
}
}
else// User defends
{
playerHealth = playerHealth - defendAtk;
if (playerHealth < 0)
{
Console.WriteLine(monsterName + " has defeated you!");
}
else
{
Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
}
}
}
// End Game
Console.WriteLine("Game Over!");
Console.ReadLine();
我认为这是相关的代码,但以防万一,这是整个程序。
namespace textBasedGame
{
class Program
{
static void Main(string[] args)
{
//Variables
string name;
string monsterName;
int playerAtk = 5;
int monsterAtk = 2;
int defendAtk = 1;
int playerHealth = 2;
int monsterHealth = 3;
// Gathers information from the user
Console.WriteLine("What is your name?");
name = Console.ReadLine();
Console.WriteLine("What is your enemies name?");
monsterName = Console.ReadLine();
//Story Intro
Console.WriteLine("You are hunting in the swamp and in the darkness of the overgrowth you see a movement.");
Console.WriteLine("You creep closer to the silouette.");
Console.WriteLine("A little closer....");
Console.WriteLine("It looks familiar....");
Console.WriteLine("It is " + monsterName + "!!!");
Console.WriteLine("Now is your chance!");
// Main Game Loop
while (playerHealth > 0 && monsterHealth > 0)
{
bool playerAct = askAction();
if (playerAct == true)// User attacks
{
monsterHealth = monsterHealth - playerAtk;
if (monsterHealth < 0)
{
Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
}
else
{
Console.WriteLine(monsterName + " takes a mighty swing back at you!");
playerHealth = playerHealth - monsterAtk;
}
}
else// User defends
{
playerHealth = playerHealth - defendAtk;
if (playerHealth < 0)
{
Console.WriteLine(monsterName + " has defeated you!");
}
else
{
Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
}
}
}
// End Game
Console.WriteLine("Game Over!");
Console.ReadLine();
}
static bool askAction()//Displays HP and asks for user action. Returns true or false
{
bool move;
Console.WriteLine("What would you like to do?(type: attack or defend)");
string playerAction = Console.ReadLine();
if (playerAction == "attack")
{
return move = true;
}
else
{
return move = false;
}
}
}
}
答案 0 :(得分:8)
while (playerHealth > 0 && monsterHealth > 0)
if (playerHealth < 0)
只要玩家的健康状况高于零,while
循环就会继续。 if
语句在其健康状况低于零时触发。如果他们的健康状况正好为零会怎样?
答案 1 :(得分:1)
首先,我更正了这个功能...基本上你不需要布尔&#34;移动&#34;,你没有创建它,因为你没有在函数中使用它
当你的功能是&#34; bool&#34;时,它会返回一个&#34; bool&#34;。换句话说,你可以做到
return true;
或
return false;
不需要
return move = true;
请改为尝试:
static bool askAction()//Displays HP and asks for user action. Returns true or false
{
Console.WriteLine("What would you like to do?(type: attack or defend)");
string playerAction = Console.ReadLine();
if (playerAction == "attack")
{
return true;
}
return false;
}
这是你的主循环。我猜你的怪物死于0生命值,所以我改变了标志
while (playerHealth > 0 && monsterHealth > 0)
{
bool playerAct = askAction();
if (playerAct == true)// User attacks
{
monsterHealth = monsterHealth - playerAtk;
if (monsterHealth <= 0)
{
Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
}
else
{
Console.WriteLine(monsterName + " takes a mighty swing back at you!");
playerHealth = playerHealth - monsterAtk;
}
}
else// User defends
{
playerHealth = playerHealth - defendAtk;
if (playerHealth <= 0)
{
Console.WriteLine(monsterName + " has defeated you!");
}
else
{
Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
}
}
}
哦,顺便说一句,在节目结束时,你说&#34;游戏结束&#34;,但如果玩家赢了怎么办?
答案 2 :(得分:0)
我看到的唯一逻辑错误是,您有monsterHealth = 2
和playerAtk = 5
,因此怪物在每次首次攻击时都会死亡,monsterAtk = 2;
和playerHealth=2
也是如此。同样,玩家在第一次攻击时死亡所以它已成为一场一招。
然后为了防守,在玩家健康状况为零之后,它就会存在而不会打印。