下面是同学玩过的剪刀石头布的游戏规则

核心提示设;布赢石头为x 9x+(21-7-x)5=108-279x+70-5x=94 4x=94-70 4 x=24x=6 所以布赢石头是6次然后;(108-27-69)/5=8 所以石头赢剪刀是8次c++编写石头-剪刀-布游戏 高手进

设;布赢石头为x 9x+(21-7-x)5=108-27

9x+70-5x=94

4x=94-70

4 x=24 x=6 所以布赢石头是6次

然后;(108-27-69)/5=8 所以石头赢剪刀是8次

c++编写石头-剪刀-布游戏 高手进,非常感谢

//s(表示剪刀)或r(表示石头)或p(表示纸)

#include<iostream>

using namespace std;

int main()

{ 

    char s1,s2;

    cout<<"s1=";

    cin>>s1;

    cout<<"s2=";

    cin>>s2;

    if ( (s1=='s'&&s2=='p')||(s1=='r'&&s2=='s')||(s1=='p'&&s2=='r') )

    {

        cout<<"s1 win"<<endl;

    }

    else if (s1==s2)

    {

        cout<<"s1 s2 equal"<<endl;

    }

    else

    {

        cout<<"s2 win"<<endl;

    }

    return 0;

}

用JAVA做一个剪刀,石头,布的人机猜拳游戏。

#include "stdioh"

#include "stdlibh"

#include "timeh"

//S代表石头,C代表剪刀,P代表布

void main()

{

char c1,c2;

srand((unsigned)time(NULL));

while(1)

{

printf("请您出:石头(S/s),剪刀(C/c),布(P/p)n");

c1=getchar();

while(c1!='S' && c1!='s' && c1!='C' && c1!='c' && c1!='P' && c1!='p')

{

printf("请输入规定的字符n");

getchar();

c1=getchar();

}

getchar();

if(c1=='S' || c1=='s')

{

printf("你出了石头n");c1=0;

}

else if(c1=='C' || c1=='c')

{

printf("你出了剪刀n");c1=1;

}

else

{

printf("你出了布n");c1=2;

}

c2=rand()%3;

if(c2==0) printf("电脑出了石头n");

else if(c2==1) printf("电脑出了剪刀n");

else printf("电脑出了布n");

if(c1-c2==-1 || c1-c2==2)

printf("你赢了!!!n");

else if(c1-c2==1 || c1-c2==-2)

printf("你输了n");

else

printf("平手,继续n");

}

}

编写这个小游戏 我们需要几个类 

1、第一个 Person 类

import javautilScanner;

/

@copyright 2018 sugarsLabcom All rights reserved

@author jingfeiwu

@date 2018年11月16日

@version 10

@ClassName Person

@description 用户类 用来计算用户输入

/

public class Person {

public static final Person me = new Person();

private int n = 0;

/

@copyright 2018 sugarsLabcom All rights reserved

@author jingfeiwu

@date 2018年11月16日

@version 10

@description 用户输入的 指令

@return

/

public int input() {

Systemoutprintln("请输入:石头,剪刀,布n输入:@退出 退出系统");

@SuppressWarnings("resource")

Scanner scanner = new Scanner(Systemin);

String s = scannernext();

// s 里面存着 用户输入的 指令 切记这里不要使用 sequals() 而是写 "指令"equals() 这么写 是为了避免空指针

if ("石头"equals(s)) {

n = 1;

} else if ("剪刀"equals(s)) {

n = 2;

} else if ("布"equals(s)) {

n = 3;

} else if ("@退出"equals(s)) {

Systemoutprint("系统退出了");

Systemexit(0);

}

return n;

}

}

2、Computer 类

/

@copyright 2018 sugarsLabcom All rights reserved

@author jingfeiwu

@date 2018年11月16日

@version 10

@ClassName Computer

@description 游戏中电脑类 用来产生随机数

/

public class Computer {

public static final Computer me = new Computer();

/

@copyright 2018 sugarsLabcom All rights reserved

@author jingfeiwu

@date 2018年11月16日

@version 10

@description TODO

@return {int} 返回值为int 类型

/

public int random() {return (int) (Mathrandom() 3 + 1);}

}

3、Game类

/

@author jingfeiwu

@date 2018年11月16日

@version 10

@ClassName Game

@description 游戏类 用来计算游戏结果

/

public class Game {

/

@author jingfeiwu

@date 2018年11月16日

@version 10

@description 返回 人机交互结果

@param n

            {int} 用户输入 的标识 石头 为 1 剪刀 为 2 布 为 3

@param m

            {int} 电脑产生的随机数 石头 为 1 剪刀 为 2 布 为 3

/

public void result(int n, Integer m) {

String res = "";

if (mintValue() == 1)

res = "石头";

else if (mintValue() == 2)

res = "剪刀";

else

res = "布";

if (n == m) {

Systemoutprintln("平了 computer出" + res);

} else {

if (m == 1) {

if (n == 2)

Systemoutprintln("你输了 computer出 " + res);

else if (n == 3)

Systemoutprintln("你赢了 computer出 " + res);

} else if (m == 2) {

if (n == 1)

Systemoutprintln("你赢了 computer出 " + res);

else if (n == 3)

Systemoutprintln("你输了 computer出 " + res);

} else if (m == 3) {

if (n == 1)

Systemoutprintln("你输了 computer出 " + res);

else if (n == 2)

Systemoutprintln("你赢了 computer出 " + res);

}

}

}

public static void main(String[] args) {

while (true) {

Game gamer = new Game();

gamerresult(Personmeinput(), Computermerandom());

}

}

}

如下是程序运行截图

 
友情链接
鄂ICP备19019357号-22