在c++中for循环里if和break的问题
为什么以下代码在执行的时候怎么都不会输出player1的提示信息啊,求助!代码如下————————————————————————————————————#include...
为什么以下代码在执行的时候怎么都不会输出player1的提示信息啊,求助!
代码如下
————————————————————————————————————
#include <iostream>
#include <string>
using namespace std;
struct player
{
string name;
int hp;
int atk;
int def;
};
int main()
{
cout<<"请依次输入第一个角色的名字"<<endl; //血量、攻击力、防御力
player player1;
getline(cin,player1.name);
cout<<"请为其指定血量、攻击力、防御力"<<endl;
cin>>player1.hp>>player1.atk>>player1.def;
cin.get();
player player2;
cout<<"请依次输入第二个角色的名字"<<endl;
getline(cin,player2.name);
cout<<"请为其指定血量、攻击力、防御力"<<endl;
cin>>player2.hp>>player2.atk>>player2.def;
cout<<"您创建的角色名以及信息如下"<<endl;
cout<<"角色名:"<<player1.name<<endl<<"血量:"<<player1.hp<<endl<<"攻击力:"<<player1.atk<<endl<<"防御力:"<<player1.def<<endl<<endl;
cout<<"角色名:"<<player2.name<<endl<<"血量:"<<player2.hp<<endl<<"攻击力:"<<player2.atk<<endl<<"防御力:"<<player2.def<<endl<<endl;
int wait=0;
int I=0;
for (I=1000000;i>1;i=I-1)
{
player1.hp = player1.hp-(player2.atk-player1.def);
player2.hp = player2.hp-(player1.atk-player2.def);
if(player2.hp<1)
{
cout<<player2.name<<"death";
break;
}
else if(player1.hp<1)
{
cout<<player1.name<<"death";
break;
}
else{;}
}
cin>>wait;
return 0 ;
} 展开
代码如下
————————————————————————————————————
#include <iostream>
#include <string>
using namespace std;
struct player
{
string name;
int hp;
int atk;
int def;
};
int main()
{
cout<<"请依次输入第一个角色的名字"<<endl; //血量、攻击力、防御力
player player1;
getline(cin,player1.name);
cout<<"请为其指定血量、攻击力、防御力"<<endl;
cin>>player1.hp>>player1.atk>>player1.def;
cin.get();
player player2;
cout<<"请依次输入第二个角色的名字"<<endl;
getline(cin,player2.name);
cout<<"请为其指定血量、攻击力、防御力"<<endl;
cin>>player2.hp>>player2.atk>>player2.def;
cout<<"您创建的角色名以及信息如下"<<endl;
cout<<"角色名:"<<player1.name<<endl<<"血量:"<<player1.hp<<endl<<"攻击力:"<<player1.atk<<endl<<"防御力:"<<player1.def<<endl<<endl;
cout<<"角色名:"<<player2.name<<endl<<"血量:"<<player2.hp<<endl<<"攻击力:"<<player2.atk<<endl<<"防御力:"<<player2.def<<endl<<endl;
int wait=0;
int I=0;
for (I=1000000;i>1;i=I-1)
{
player1.hp = player1.hp-(player2.atk-player1.def);
player2.hp = player2.hp-(player1.atk-player2.def);
if(player2.hp<1)
{
cout<<player2.name<<"death";
break;
}
else if(player1.hp<1)
{
cout<<player1.name<<"death";
break;
}
else{;}
}
cin>>wait;
return 0 ;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询