C++ 有没有办法尽量减少IF声明的数量?
我的代码中充满了If语句,我想减少If语句的数量,如果不是全部去掉的话。我该怎么做?任何其他提示和改进都非常感谢! 我刚刚开始学习编写代码,并跟随导师写了这篇文章,但我很难理解如何最大限度地减少IFs的数量C++ 有没有办法尽量减少IF声明的数量?,c++,C++,我的代码中充满了If语句,我想减少If语句的数量,如果不是全部去掉的话。我该怎么做?任何其他提示和改进都非常感谢! 我刚刚开始学习编写代码,并跟随导师写了这篇文章,但我很难理解如何最大限度地减少IFs的数量 void Combat() { //combat simiulator CombatHUD(); int userAttack; int userDamage = 1000; //8 * level / 2; int monsterAttack = 6
void Combat() {
//combat simiulator
CombatHUD();
int userAttack;
int userDamage = 1000; //8 * level / 2;
int monsterAttack = 6 * monsterlevel / 2;
if (character.totalHealth >= 1 && monsterHealth >= 1) {
std::cout << "\n";
std::cout << "1. Attack\n";
std::cout << "2. Block\n";
std::cout << "3. Run\n";
std::cout << "\n";
std::cin >> userAttack;
if (userAttack == 1) {
//User Attack
std::cout << "Attacking... you did " << userDamage << " to the " << currentMonster << std::endl;
monsterHealth = monsterHealth - userDamage;
Sleep(1000);
CombatHUD();
if (monsterHealth >= 1) {
std::cout << "Monster is attacking... \n";
character.totalHealth = character.totalHealth - monsterAttack;
std::cout << "You recieved " << monsterAttack << " damage " << std::endl;
if (character.totalHealth <= 0) {
character.totalHealth = 0;
system("cls");
std::cout << "You died! Game over!";
Sleep(2000);
exit(0);
}
}
else if (monsterHealth <= 0) {
monsterHealth = 0;
if (character.level != character.maxlevel) {
character.current_xp += monsterXp;
LevelUp();
}
std::cout << "\n";
std::cout << "You defeated " << currentMonster << " you get " << monsterXp << "XP.\n";
Sleep(2000);
HUD();
}
Sleep(1000);
Combat();
}
else if (userAttack == 2) {
//User Block. broken?
std::cout << "Blocking\n";
int i = rand() % 100 + 1;
if (i >= 50) {
std::cout << "You blocked the incoming attack\n";
character.heal = character.level * 10 / 2;
std::cout << "you have been healed for " << character.heal << std::endl;
character.totalHealth += character.heal;
Sleep(1000);
Combat();
}
}
else if (userAttack == 3) {
//User escape
std::cout << "You try to run\n";
int x = rand() % 100 + 1;
if (x >= 50) {
std::cout << "You run away\n";
HUD();
}
else {
std::cout << "You failed to run away \n";
std::cout << "Monster does a critical hit! \n";
character.totalHealth -= monsterAttack + 10;
std::cout << "You suffered " << monsterAttack + 10 << "Your current health is " << character.totalHealth << std::endl;
Sleep(2000);
Combat();
}
}
else {
std::cout << "Invalid Input\n";
Sleep(500);
Movement();
}
}
}
void Movement() {
//user movement. enhance?
int choice;
std::cout << "\n\n";
std::cout << "1. Move forward\n";
std::cout << "2. Chill\n";
std::cout << "3. Move Backwards\n";
std::cout << "\n";
std::cin >> choice;
if (choice == 1) {
int temp = rand() % 100 + 1;
std::cout << "You begin moving forward...\n";
if (temp >= 50) {
Monster();
std::string tempName = monsterName[rand() % currentMonsterNames];
std::cout << "A " << tempName << "! Get ready to fight it!\n";
currentMonster = tempName;
Sleep(1000);
Combat();
}
std::cout << "You find nothing\n";
Sleep(1000);
HUD();
}
else if (choice == 2) {
std::cout << "You want to chill for the rest of the day\n";
if (character.totalHealth <= 99) {
character.totalHealth += 10 * character.level;
}
std::cout << "You healed by chilling Health is now " << character.totalHealth << std::endl;
Sleep(1000);
HUD();
}
else if (choice == 3) {
std::cout << "You begin moving backwards...\n";
std::cout << "You're going no where\n";
Sleep(2000);
system("cls");
}
else {
std::cout << "Invalid Input\n";
Sleep(500);
Movement();
}
}
void Monster() {
//monster creator
monsterHealth = 30; {
monsterlevel = (rand() % 3) + character.level;
}
monsterHealth = (rand() % 30) * monsterlevel;
monsterXp = monsterHealth;
if (monsterHealth == 0)
Monster();
if (monsterlevel == 0)
Monster();
}
void LevelUp() {
//level up mechanic
if (character.current_xp >= character.xp_to_level) {
character.xp_to_level += floor(character.level + 15 * pow(2, character.level / 7));
character.totalHealth = floor(character.totalHealth + 10 * pow(2, character.level / 8));
if (character.level >= character.minLevel && character.level <= character.maxlevel) {
character.level++;
}
else {
character.level = 5;
}
character.maxHealth = character.totalHealth;
std::cout << "Ba Da Bing! You've leveled up! You're max health has increased!" << std::endl;
Sleep(2000);
LevelUp();
}
Sleep(2000);
HUD();
}
void战斗(){
//战斗模拟机
CombatHUD();
int用户攻击;
int userDamage=1000;//8*级/2;
智力怪物攻击=6*怪物等级/2;
如果(character.totalHealth>=1&&monsterHealth>=1){
std::cout这是您的移动功能,表示为开关:
void Movement() {
//user movement. enhance?
int choice;
std::cout << "\n\n";
std::cout << "1. Move forward\n";
std::cout << "2. Chill\n";
std::cout << "3. Move Backwards\n";
std::cout << "\n";
std::cin >> choice;
switch (choice)
{
case 1:
int temp = rand() % 100 + 1;
std::cout << "You begin moving forward...\n";
if (temp >= 50) {
Monster();
std::string tempName = monsterName[rand() % currentMonsterNames];
std::cout << "A " << tempName << "! Get ready to fight it!\n";
currentMonster = tempName;
Sleep(1000);
Combat();
}
std::cout << "You find nothing\n";
Sleep(1000);
HUD();
break;
case 2:
std::cout << "You want to chill for the rest of the day\n";
if (character.totalHealth <= 99) {
character.totalHealth += 10 * character.level;
}
std::cout << "You healed by chilling Health is now " << character.totalHealth << std::endl;
Sleep(1000);
HUD();
break;
case 3:
std::cout << "You begin moving backwards...\n";
std::cout << "You're going no where\n";
Sleep(2000);
system("cls");
break;
default:
std::cout << "Invalid Input\n";
Sleep(500);
Movement();
}
}
void移动(){
//用户移动。增强?
智力选择;
STD::CUT这是一个非常广泛的、开放式的软件设计问题,不仅仅是C++。
在大多数情况下,您将无法删除
if
s,因为您有条件逻辑——这很好。但是,您可以遵循一些设计实践,以减少每个函数的if
语句:
- 将逻辑分解为更小的函数,以隔离它们的关注点。例如,每个不同的
作战
动作都可以是单独的函数。这不会减少if
s,而是将每个函数的嵌套量限制在逻辑上需要的位置
- 查找重复的代码或模式,并将它们提取到函数中。同样,这有助于将
if
s限制在需要它们的地方
- 使用
开关代替if
/else
梯形图
- 使用较小的函数,您可以更改
if
语句以检查提前终止情况,并提前返回以避免嵌套。这会使嵌套更短,因为提前返回后的主分支隐式地是else
。例如:
void check\u user\u death()
{
//而不是“如果”(character.totalHealth欢迎使用SO!您的代码是否按照您的意愿工作,并且只想重构它?您当然应该学习如何使用switch
语句。您可能会希望将代码分解为更多的函数。看起来您的一些函数做得太多了。另外,当您使用时,不要养成使用递归的习惯应该使用循环。