close

< C++完整教學目錄 >

 

※ 綜合應用:一個很簡單的剪刀石頭布遊戲

    string hand[3]  = {"剪刀","石頭",""};

    for (int i=1;i>0;i++)

    {

        int A = (i%3) + 1;

        int B;

        string s1 = "電腦出";

        string s2 = ",請問您要出?(1出剪刀,按2出石頭,按3出布,按其他離開遊戲)";

        cout<<s1+hand[A-1]+s2<<endl;

        cin>>B;

        if (A==1)

        {

            if (B==1){cout<<"雙方平手!"<<endl;}

            else if (B==2){cout<<"你贏了!"<<endl;}

            else if (B==3){cout<<"你輸了!"<<endl;}

            else {break;}

        }

        else if (A==2)

        {

            if (B==1){cout<<"你輸了!"<<endl;}

            else if (B==2){cout<<"雙方平手!"<<endl;}

            else if (B==3){cout<<"你贏了!"<<endl;}

            else {break;}

        }

        else if (A==3)

        {

            if (B==1){cout<<"你贏了!"<<endl;}

            else if (B==2){cout<<"你贏了!"<<endl;}

            else if (B==3){cout<<"雙方平手!"<<endl;}

            else {break;}

        }     

    }

上一頁--迴圈 

arrow
arrow

    埃伯 發表在 痞客邦 留言(0) 人氣()