私のプログラムに問題があり、何か助けが必要です。私のプログラムは、誰が最初にやって来るのか、ユーザーか、コンピュータをランダムに選ぶことになっています。プログラムはちょうど良いことです。使用したい武器を入力すると、適切な計算が行われ、コンピュータが最初に行かれればランダムに生成されたダメージの量が減算されます。最初のユーザーかコンピュータかに関係なく、Enterキーを押すとプログラムが停止します。ユーザーまたはコンピュータの状態がゼロになるまで続行されるはずです。私は、私のコードがこれが起こらないように助けを得ることができるかどうか疑問に思っていました。どんな助けも高く評価されます。私はC++言語の初心者です。ありがとうございました!XCode C++プログラムが適切なループを実行していない
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main() // Setting the "stage" for the code to follow.
{
int Player_Health = 100; // Creating the necessary variables for the program.
int Comp_Health = 100;
int Turn;
int Weapon_Selection;
int Comp_Weapon;
int Cannon_Dmg;
int Grenade_Dmg;
int Rifle_Dmg;
int Player_Cannon_Ammo = 3;
int Player_Grenade_Ammo = 4;
int Comp_Cannon_Ammo = 3;
int Comp_Grenade_Ammo = 4;
srand(static_cast<unsigned int>(time(0)));
Turn = rand() % 2; // Randomizing who gets to go first.
while(Player_Health >= 0 && Comp_Health >= 0)
{
if(Turn == 0) // Player gets to go first.
{
cout << "You get to go first!\n";
cout << "Select a weapon using number keys 1, 2, or 3.\n";
cout << "1. Cannon\n";
cout << "2. Grenade\n";
cout << "3. Rifle\n";
cout << "\n";
cout << "You select: ";
cin >> Weapon_Selection;
while((Weapon_Selection < 1 || Weapon_Selection > 3) || (Weapon_Selection == 1 && Player_Cannon_Ammo == 0)
|| (Weapon_Selection == 2 && Player_Grenade_Ammo == 0))
{
cout << "Please enter a valid option.\n";
cout << "You select: ";
cin >> Weapon_Selection;
}
switch(Weapon_Selection)
{
case 1: // Player chooses to shoot the cannon.
Cannon_Dmg = 10 + rand() % 6;
Comp_Health = Comp_Health - Cannon_Dmg;
cout << "You caused " << Cannon_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout <<" The computer's health is " << Comp_Health << endl;
Player_Cannon_Ammo = Player_Cannon_Ammo - 1;
break;
case 2: // Player chooses to lob a grenade.
Grenade_Dmg = 7 + rand() % 6;
Comp_Health = Comp_Health - Grenade_Dmg;
cout << "You caused " << Grenade_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Player_Grenade_Ammo = Player_Grenade_Ammo - 1;
break;
case 3: // Player chooses to shoot the rifle.
Rifle_Dmg = 3 + rand() % 6;
Comp_Health = Comp_Health - Rifle_Dmg;
cout << "You caused " << Rifle_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
break;
}
}
else // Computer gets to go first.
{
Comp_Weapon = rand() % 3;
switch(Comp_Weapon) // Computer randomly selects a weapon.
{
case 1:
Cannon_Dmg = 10 + rand() % 6;
Player_Health = Player_Health - Cannon_Dmg;
cout << "Your enemy used a cannon and caused " << Cannon_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Comp_Cannon_Ammo = Comp_Cannon_Ammo - 1;
break;
case 2:
Grenade_Dmg = 7 + rand() % 6;
Player_Health = Player_Health - Grenade_Dmg;
cout << "Your enemy used a grenade and caused " << Grenade_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Comp_Grenade_Ammo = Comp_Grenade_Ammo - 1;
break;
case 3:
Rifle_Dmg = 3 + rand() % 6;
Player_Health = Player_Health - Rifle_Dmg;
cout << "Your enemy used a rifle and caused " << Rifle_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
break;
}
}
if (Comp_Health < 0)
cout << "Congratulations, you beat your enemy!" << endl;
if (Player_Health < 0)
cout << "You have been defeated by your enemy!" << endl;;
cin.ignore(2);
return 0;
}
}
デバッガを試しましたか? –
エラーはありません。それはループの位置と関係がある。 – Zjm4192
しかし、あなたのコードの動作はあなたの期待と異なりますか?このような状況では、デバッガは使用するツールです。 –