今天,用c++制作一个好玩的石头剪刀布玩玩(需要在dev-c++编译并运行,运行前关闭360,因为会误报)

首先,先写上c++大部分程序的标配

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

然后,写上using namespace std;

//头文件
using namespace std;

最后,就是整个最重要的东西——主要的代码

int main(){
    int r,input_num;
    cout<<"========石头剪刀布小游戏========";
    string computer,person;
    srand(time(0));
    r = rand()%3+1;
    switch(r){
        case 1:computer="石头";break;
        case 2:computer="剪刀";break;
        case 3:computer="布";break;
    }
    cout<<"电脑已出拳"<<endl;
    cout<<"请你出拳(1.石头 2.剪刀 3.布)"<<endl;
    cin>>input_num;
    switch(input_num){
        case 1:person="石头";break;
        case 2:person="剪刀";break;
        case 3:person="布";break;
    }
    cout<<"电脑出的拳:"<<computer<<",你出的拳:"<<person<<endl;
    if(input_num==r)
    {
        cout<<"平局-_-"<<endl;
    }
    else if(input_num==1&&r==2)
    {
        cout<<"你赢了^_^"<<endl;
    }
    else if(input_num==2&&r==3)
    {
        cout<<"你赢了^_^"<<endl;
    }
    else if(input_num==3&&r==1)
    {
        cout<<"你赢了^_^"<<endl;
    }    
    else
    {
        cout<<"你输了=_="<<endl;
    }
    return 0;
}

完整代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
//头文件
using namespace std;

int main(){
    int r,input_num;
    cout<<"========石头剪刀布小游戏========";
    string computer,person;
    srand(time(0));
    r = rand()%3+1;
    switch(r){
        case 1:computer="石头";break;
        case 2:computer="剪刀";break;
        case 3:computer="布";break;
    }
    cout<<"电脑已出拳"<<endl;
    cout<<"请你出拳(1.石头 2.剪刀 3.布)"<<endl;
    cin>>input_num;
    switch(input_num){
        case 1:person="石头";break;
        case 2:person="剪刀";break;
        case 3:person="布";break;
    }
    cout<<"电脑出的拳:"<<computer<<",你出的拳:"<<person<<endl;
    if(input_num==r)
    {
        cout<<"平局-_-"<<endl;
    }
    else if(input_num==1&&r==2)
    {
        cout<<"你赢了^_^"<<endl;
    }
    else if(input_num==2&&r==3)
    {
        cout<<"你赢了^_^"<<endl;
    }
    else if(input_num==3&&r==1)
    {
        cout<<"你赢了^_^"<<endl;
    }    
    else
    {
        cout<<"你输了=_="<<endl;
    }
    return 0;
}

更多推荐

50行代码,让你制作出一个好玩的石头剪刀布(人机对战)