欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单石头剪刀布游戏

程序员文章站 2022-06-24 17:41:14
本文实例为大家分享了java实现简单石头剪刀布游戏的具体代码,供大家参考,具体内容如下问题描述alice, bob和cindy一起玩猜拳的游戏。和两个人的猜拳类似,每一轮,他们会从石头、剪刀、布中各自...

本文实例为大家分享了java实现简单石头剪刀布游戏的具体代码,供大家参考,具体内容如下

问题描述

alice, bob和cindy一起玩猜拳的游戏。和两个人的猜拳类似,每一轮,他们会从石头、剪刀、布中各自选一个出拳,基本的胜负规则是石头赢剪刀、剪刀赢布、布赢石头。如果一轮中正好可以分成胜负两边,则负边的每个人要支付给胜边的每个人一块钱。如果无法分成胜负两边,则都不出钱。比如,如果alice出石头,而bob和cindy都出布,则alice要分支付bob和cindy一块钱。再如,如果alice出石头, bob出剪刀, cindy出布,则都不出钱。他们三人共进行了n轮游戏,请问最后每个人净赚多少钱?即赚的钱减去支付的钱是多少?

代码

package ring1270.pra.java01;
import java.util.scanner;
/**
 * finger-guessing game: * n:number of games * a: person a's money * b: person b's money * c: person c's money * 0: stand for stone * 1: stand for scissor * 2: stand for cloth * rule1: two persons give the same result means game over * rule2: the money add 1 everytime which win * rule3:the money less 1 everytime which fail * */public class d_fingerguessinggame {
  public static void main(string[] args) {
    int a = 0;
    int b = 0;
    int c = 0;
    scanner scanner = new scanner(system.in);
    system.out.printf("the number of game:");
    int n = scanner.nextint();
    stringbuffer stringbuffer = new stringbuffer();
    for (int i = 0; i <= n; i++) {
      string s = scanner.nextline();
      char[] d = s.tochararray();
      for (int j = 0; j < d.length; j++) {
        //a and b success
 if (d[0] == d[1] && d[0] != d[2]) {
          if ('0' == d[0] && '1' == d[2]) {
            a++;
            b++;
            c -= 2;
          }
          else if ('1' == d[0] && '2' == d[2]) {
            a++;
            b++;
            c -= 2;
          }
          else if ('2' == d[0] && '0' == d[2]) {
            a++;
            b++;
            c -= 2;
          }else {
            a--;
            b--;
            c += 2;
          }
        }
        // a and c success
 if (d[0] == d[2] && d[0] != d[1]) {
          if ('0' == d[0] && '1' == d[1]) {
            a++;
            b -= 2;
            c++;
          }
          else if ('1' == d[0] && '2' == d[1]) {
            a++;
            b -= 2;
            c++;
          }
          else if ('2' == d[0] && '0' == d[1]) {
            a++;
            b -= 2;
            c++;
          }else {
            a--;
            b += 2;
            c--;
          }
        }
        // c and b success
 if (d[1] == d[2] && d[1] != d[0]) {
          if ('0' == d[1] && '1' == d[0]) {
            a -= 2;
            b++;
            c++;
          }
          else if ('1' == d[1] && '2' == d[0]) {
            a -= 2;
            b++;
            c++;
          }
          else if ('2' == d[1] && '0' == d[0]) {
            a -= 2;
            b++;
            c++;
          }
          else {
            a += 2;
            b--;
            c--;
          }
        }
        break;
      }
    }
    system.out.println(a);
    system.out.println(b);
    system.out.println(c);
  }
}

运行截图

java实现简单石头剪刀布游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇: tools

下一篇: Tools