「java程序猜拳」java实现猜数游戏

博主:adminadmin 2023-03-18 14:11:10 487

今天给各位分享java程序猜拳的知识,其中也会对java实现猜数游戏进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java猜拳游戏 (3局2胜)

package Demo;

import java.util.Random;

import java.util.Scanner;

public class Demo12 {

public static void main(String[] args) {

String[] str = { "石头", "剪刀", "布" };

Random ram = new Random();

int y, n, i;

while (true) {

System.out.println("菜单:\n1、开始猜拳  \n9、退出");

Scanner scan = new Scanner(System.in);

System.out.print("请选择:");

String s = scan.nextLine();

if ("1".equals(s.trim())) {

y = 0;

n = 0;

i = 0;

while (true) {

try {

System.out.println("请出拳:1、石头  2、剪刀  3、布");

int s1 = Integer.parseInt(scan.nextLine());

if (s1  0  s1  4) {

System.out.println("你  出:" + str[s1 - 1]);

int s2 = ram.nextInt(3);

System.out.println("我  出:" + str[s2]);

if (s1 == (s2 + 1)) {

System.out.println("这次是平局");

} else if ((s1 == 1  s2 == 1)

|| (s1 == 2  s2 == 2)

|| (s1 == 3  s2 == 0)) {

System.out.println("这次你赢了!");

y++;

} else if ((s1 == 1  s2 == 2)

|| (s1 == 2  s2 == 0)

|| (s1 == 3  s2 == 1)) {

System.out.println("这次你输了!");

n++;

}

if (i == 2) {

if (y  n) {

System.out.println("你赢了  " + y + ":" + n);

} else if (y  n) {

System.out.println("你输了  " + y + ":" + n);

} else {

System.out.println("平局  " + y + ":" + n);

}

break;

}

i++;

} else {

System.out.println("输入有误!");

}

} catch (Exception ex) {

System.out.println("输入有误!");

}

}

} else if ("9".equals(s.trim())) {

System.out.println("退出成功");

return;

} else {

System.out.println("指令错误~");

}

}

}

}

菜单:

1、开始猜拳

9、退出

请选择:2

指令错误~

菜单:

1、开始猜拳

9、退出

请选择:1

请出拳:1、石头  2、剪刀  3、布

2

你  出:剪刀

我  出:布

这次你赢了!

请出拳:1、石头  2、剪刀  3、布

4

输入有误!

请出拳:1、石头  2、剪刀  3、布

3

你  出:布

我  出:布

这次是平局

请出拳:1、石头  2、剪刀  3、布

1

你  出:石头

我  出:石头

这次是平局

你赢了  1:0

菜单:

1、开始猜拳

9、退出

请选择:9

退出成功

求一个java猜拳游戏程序

package test;

import java.util.Random;

import java.util.Scanner;

/**

 * 猜拳游戏思路 

 * 1、定义输入函数 

 * 2、提示用户输入猜拳数值 

 * 3、定义随机一个数作为电脑数值 

 * 4、判断[用户输入数值]与 [电脑随机数值] 

 * 5、能够相等就是打平,不能相等就利用、||逻辑符判断输赢 

 * 6、设定数值1-石头 2-剪刀  3-布

 */

public class CaiQuanYouXi {

 public static void main(String[] args) {

  Scanner in=new Scanner(System.in);//定义输入函数in,Scanner包功能,输入数值用的

  System.out.println("--------------猜拳游戏---------------");

  System.out.println("请输入一个数值:1、石头 2、剪刀 3、布");//提示输入数值 

  System.out.println(" ");//空行

  int x=in.nextInt();//让用户输入X的数值 

  Random on=new Random();//定义电脑的随机数值的函数on 

  int y=on.nextInt(3)+1;//定义y随机函数数值范围(1--3)

  if(x=4||x==0){   //判断用户是否输入非1--3范围 

   System.out.println("亲,请正确输入:1、石头 2、剪刀 3、布。你输入了:"+x);  

  }else{   

   /*下面是判断用户输入x的数值 嵌套if*/ 

   if(x==y){   

    if(x==1){ //判断打平的情况 

     System.out.println("你:石头------电脑:石头    PK:很幸运打平手"); 

    }else if(x==2){ 

     System.out.println("你:剪刀------电脑:剪刀   PK:很幸运打平手"); 

    }else { 

     System.out.println("你:布------电脑:布    PK:很幸运打平手"); 

    } 

   }else if(x==1y==2||x==2y==3||x==3y==1){ //开始判断赢的情况 

    if(x==1y==2){ 

     System.out.println("你:石头------电脑:剪刀    PK:恭喜您,赢了!"); 

    }else if(x==2y==3){ 

     System.out.println("你:剪刀------电脑:布   PK:恭喜您,赢了!"); 

    }else {

     System.out.println("你:布------电脑:石头    PK:恭喜您,赢了!");

    } 

   }else {//开始判断输的情况 

    if(x==1y==3){ 

     System.out.println("你:石头------电脑:布    PK:很遗憾,输了!"); 

    }else if(x==2y==1){ 

     System.out.println("你:剪刀------电脑:石头    PK:很遗憾,输了!"); 

    }else { 

     System.out.println("你:布------电脑:剪刀    PK:很遗憾,输了!"); 

    } 

   }

  }

 }

}

运行后的效果展示:

--------------猜拳游戏---------------

请输入一个数值:1、石头 2、剪刀 3、布

1

你:石头------电脑:布    PK:很遗憾,输了!

--------------猜拳游戏---------------

请输入一个数值:1、石头 2、剪刀 3、布

4

亲,请正确输入:1、石头 2、剪刀 3、布。你输入了:4

java代码猜拳游戏相关代码请教

comp是电脑产生的随机数字(电脑出的拳),people 是人出的拳。 因为剪刀石头布只有 1 2 3

。如果电脑的数字比的你刚好大1,就是它比你的大。 如21,32对应就是(石头大于剪刀,布大于石头)。 但也有可能是剪刀大于布 。 那么剪刀的位子是1 ,布的位子是3. 所以当电脑数字减你的数字等于2时 就是(电脑出的布 ,你出的石头这样的情况了)。

所以是if((comp-people)==-1||(comp-people)==2) 这两者结合就是你赢的时候

Java使用循环,实现猜拳游戏统计多少局及胜率?

为了让游戏有参与感,并体现java面对对象的思想,我先创建一个Player选手类,包含选手的名字playerName还有出拳方法guess()。出拳时采用随机获取0、1和2的方式分别代表石头、剪刀和布,代码如下:

public class Player {

private String playerName;

public Player(String playerName) {

this.playerName = playerName;

}

public String getPlayerName() {

return playerName;

}

//出拳方法 0-石头 1-剪刀 2-布

public int guess() {

//随机获取0、1、2

int num = new Random().nextInt(3);

if (num == 0) {

System.out.print("选手" + this.playerName + "出的是石头 ");

} else if (num == 1) {

System.out.print("选手" + this.playerName + "出的是剪刀 ");

} else if (num == 2) {

System.out.print("选手" + this.playerName + "出的是布 ");

}

return num;

}

}

然后在主类中,首先要输入对局的总数,然后创建两名选手进行pk,在pk()方法中制定了获胜规则,详见代码注释。最终统计并利用BigDecimal计算胜率(BigDecimal可以很完美的解决整数除法及其四舍五入保留小数的问题):

public class Main {

public static void main(String[] args) {

System.out.println("请输入本局局数:");

Scanner scanner = new Scanner(System.in);

int sum = scanner.nextInt();

//创建结果数组,resultArray[0]代表p1的获胜局数,resultArray[1]代表p2的获胜局数,resultArray[2]代表平局局数

int[] resultArray = new int[3];

//创建两名选手

Player p1 = new Player("张三");

Player p2 = new Player("李四");

for (int i = 0; i sum; i++) {

//根据总局数进行pk

int result = pk(p1, p2);

if (result == 1) {

resultArray[0]++;

} else if (result == -1) {

resultArray[1]++;

} else {

resultArray[2]++;

}

}

System.out.println("");

System.out.println("最终结果统计:");

System.out.println("选手[" + p1.getPlayerName() + "]获胜局数为:" + resultArray[0] + ",胜率为:" +

new BigDecimal(resultArray[0]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + "%");

System.out.println("选手[" + p2.getPlayerName() + "]获胜局数为:" + resultArray[1] + ",胜率为:" +

new BigDecimal(resultArray[1]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + "%");

System.out.println("平局局数为:" + resultArray[2] + ",平局率为:" +

new BigDecimal(resultArray[2]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + "%");

}

//0-石头 1-剪刀 2-布

//return 0:平局 1:p1获胜 -1:p2获胜

private static int pk(Player p1, Player p2) {

System.out.println("--------------------");

int a = p1.guess();

int b = p2.guess();

System.out.print("\n对局结果:");

//出拳相同平局

if (a == b) {

System.out.println("平局");

return 0;

}

//p1获胜条件:p1出石头时p2出剪刀,p1出剪刀时p2出步,p1出布时p2出石头

else if ((a == 0 b == 1) || (a == 1 b == 2) || (a == 2 b == 0)) {

System.out.println("选手[" + p1.getPlayerName() + "]获胜");

return 1;

}

//p2获胜条件:p1出石头时p2出布,p1出剪刀时p2出石头,p1出布时p2出剪刀

else if ((a == 0 b == 2) || (a == 1 b == 0) || (a == 2 b == 1)) {

System.out.println("选手[" + p2.getPlayerName() + "]获胜");

return -1;

} else {

//因为规定了随机数产生0、1、2,所以其实不会走到本分支

throw new IllegalArgumentException("本局无效");

}

}

}

对局5局的运行结果:

我这里就只能统计当前游戏的数据了,如果你想统计多局游戏总的胜率信息,那么需要将每一局的比赛结果写到txt文件里,最终根据txt文件内容统计即可。

求JAVA人机猜拳的代码,类似一下界面。

自己纯手打,老半天才弄出来啊

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;

import java.util.Random;

import javax.swing.AbstractAction;

import javax.swing.Box;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Demo2 extends JFrame {

private JLabel lb1, lb2, lb3, lb4; // 提示标签

private JTextField ta1, ta2;// 两个文本框

private JButton b1, b2, b3; // 三个按钮

private JPanel p1, p2; // 两个JPanel面板

public Demo2() {

// 初始化所有组件

lb1 = new JLabel("欢迎使用人机猜拳程序");

lb2 = new JLabel("你出拳: ");

lb3 = new JLabel("电脑出拳:");

lb4 = new JLabel("结果");

ta1 = new JTextField();

ta1.setPreferredSize(new Dimension(60, 60)); // 设置大小

ta1.setEditable(false);//设置不可编辑

ta2 = new JTextField();

ta2.setPreferredSize(new Dimension(60, 60));

ta2.setEditable(false);//设置不可编辑

b1 = new JButton("剪刀");

b2 = new JButton("石头");

b3 = new JButton("布");

p1 = new JPanel();

p2 = new JPanel();

// 设置第一个面板内容

Box box = Box.createVerticalBox();

Box box1 = Box.createHorizontalBox();

box1.add(lb2);

box1.add(ta1);

box1.add(lb3);

box1.add(ta2);

box.add(lb1);

box.add(Box.createVerticalStrut(40));

box.add(box1);

box.add(Box.createVerticalStrut(10));

box.add(lb4);

box.add(new JLabel());

p1.add(box);

// 设置第二个面板

p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器

p2.setPreferredSize(new Dimension(0, 60));

GridBagConstraints g2 = new GridBagConstraints();

g2.fill = GridBagConstraints.BOTH;

g2.weightx = 1.0;

g2.weighty = 1.0;

g2.gridx = 0;

g2.gridy = 0;

p2.add(b1, g2);

g2.gridx = 1;

p2.add(b2, g2);

g2.gridx = 2;

p2.add(b3, g2);

//为3个按钮添加事件

b1.addActionListener(new buttonAction());

b2.addActionListener(new buttonAction());

b3.addActionListener(new buttonAction());

this.getContentPane().add(p1);

this.getContentPane().add(p2, BorderLayout.SOUTH);

this.setTitle("机器人猜拳游戏");

this.setSize(300, 300);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//事件类

class buttonAction extends AbstractAction{

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==b1){

ta1.setText("剪刀");

init(ta1.getText());

}else if(e.getSource()==b2){

ta1.setText("石头");

init(ta1.getText());

}else if(e.getSource()==b3){

ta1.setText("布");

init(ta1.getText());

}

}

// 模拟电脑出拳,产生三个随机数。0代表剪刀,1代表石头,2代表布

public String getQuan(){

String str="";

int num=new Random().nextInt(3) ;

if(num==0){

str="剪刀";

}else if(num==1){

str="石头";

}else if(num==2){

str="布";

}

return str;

}

// 判断输赢方法

public String isying(String s1,String s2){

String s="";

if(s1.equals(s2)){

s="平局";

}else if(s1.equals("剪刀")s2.equals("布")){

s="你赢";

}else if(s1.equals("石头")s2.equals("剪刀")){

s="你赢";

}else if(s1.equals("布")s2.equals("石头")){

s="你赢";

}else{

s="电脑赢";

}

return s;

}

public void init(String wo){

String sy=""; // 保存输赢结果

String dncq=getQuan(); //电脑出拳

if(wo.equals(dncq)){

sy="平局";

}else if(wo.equals("剪刀")dncq.equals("布")){

sy="你赢";

}else if(wo.equals("石头")dncq.equals("剪刀")){

sy="你赢";

}else if(wo.equals("布")dncq.equals("石头")){

sy="你赢";

}else{

sy="电脑赢";

}

ta2.setText(dncq);// 电脑出拳

lb4.setText("结果:"+sy);

}

}

public static void main(String[] args) {

new Demo2();

}

}

猜拳游戏java能输出游戏时间

Java实现猜拳游戏的核心在于电脑随机数的生成,Java中的随机数生成方法是:

首先引入包   import java.util.*;  然后   int r=new Random().nextInt(3);  (nextInt中的数字三代表随机数生成的个数,从零开始)

所以在猜拳的输入中需要有0、1、2三个数字代替,如果要输入汉字,则用if进行相应判断即可。

在实现的游戏中实现①猜拳;②记录胜负;③玩家决定游戏局数;④输出获胜、失败及平局;⑤统计总共的胜负结果(根据获胜次数判断)

①猜拳基础功能:该部分代码可以放到一个方法中,减少主函数代码量。

电脑出拳即  int r=new Random().nextInt(3);  注意:该部分一定要写在for循环内部,否则无法实现每次不同的随机数。

通过if判断双方出拳是否相等   if(a==0r==0)  else if(a==0r==1)  else if(a==0r==2)   即可实现猜拳,if内直接输出相关语句即可

②记录胜负:  定义猜拳方法为int ,通过返回值记录相关比赛的胜负情况  ,可以用0--失败;1--获胜;2--平局 进行记录,在主函数中对相应抛出的数字记录即可

if(a==0r==0){

System.out.println("The computer comes out with cloth,it was a draw. ");

return 2;

}

h=comp.compare(a,r); if (h==1) j++;

登录后复制

③玩家决定局数: 定义一个数,在循环中不大于该数即可

④输出获胜、失败及平局: j、k即胜利和失败,平局数即n-j-k。

⑤统计结果,直接用if比较i、j的数字结果即可。

主函数部分:

package SS2_5;

import java.util.*;

public class Main {

public static void main(String args[]){

Scanner scanner=new Scanner(System.in);

Compare comp=new Compare();

int h=0,j=0,k=0;

System.out.println("rules:0--cloth;1--stone;2--scissors.\nU can choose how many times you want to play:");

int n=scanner.nextInt();

for(int i=1;i=n;i++){

System.out.print("It's the "+i+" round,your turn:");

int a=scanner.nextInt();

int r=new Random().nextInt(3);

switch (a){

case 0:

h=comp.compare(a,r);

break;

case 1:

h=comp.compare(a,r);

break;

case 2:

h=comp.compare(a,r);

break;

default:

System.out.println("Wrong number!");

break;

}

if (h==1)

j++;

else if(h==0)

k++;

}

System.out.println("The total times you won are "+j+",The draw times are "+(n-j-k)+".");

if(jk)

System.out.println("You are the final winner");

else if(kj)

System.out.println("The computer is the winner.");

if(j==k)

System.out.println("The final result is draw");

}

}

登录后复制

compare方法部分

package SS2_5;

public class Compare {

public int compare(int a,int r){

int counter=0;

if(a==0r==0){

System.out.println("The computer comes out with cloth,it was a draw. ");

return 2;

}

else if(a==0r==1){

System.out.println("The computer comes out with stone, you won. ");

return 1;

}

else if(a==0r==2){

System.out.println("The computer comes out with scissor,you lost. ");

return 0;

}

else if(a==1r==0){

System.out.println("The computer comes out with cloth,you lost. ");

return 0;

}

else if(a==1r==1){

System.out.println("The computer comes out with stone,it was a draw. ");

return 2;

}

else if(a==1r==2){

System.out.println("The computer comes out with scissors,you won. ");

return 1;

}

else if(a==2r==0){

System.out.println("The computer comes out with cloth,you won. ");

return 1;

}

else if(a==2r==1){

System.out.println("The computer comes out with stone,you lost. ");

return 0;

}

else if(a==2r==2){

System.out.println("The computer comes out with scissors,it was a draw. ");

return 2;

}

else

return 0;

}

}

登录后复制

java

704拖拉机

精选推荐

广告

java写简单的猜拳游戏

2下载·0评论

2016年7月27日

用Java编写的猜拳小游戏

2029阅读·0评论·0点赞

2021年3月7日

Java猜拳游戏和Random的应用

21阅读·0评论·0点赞

2022年10月24日

java实现完整版猜拳小游戏

25下载·0评论

2018年11月22日

用python实现功能猜拳

1137阅读·2评论·3点赞

2022年7月14日

java猜拳switch计分制_java----猜拳(10局分胜负)

117阅读·0评论·1点赞

2021年3月15日

二手拖拉机交易市场,你还在高价买吗?

精选推荐

广告

利用Java编写简单的猜拳游戏

911阅读·0评论·1点赞

2022年9月8日

Java简单实现猜拳游戏

1.1W阅读·1评论·2点赞

2022年1月23日

java猜拳游戏代码_Java实现简单猜拳游戏

4496阅读·0评论·0点赞

2021年3月1日

用java来写一个简单的猜拳小游戏

890阅读·1评论·1点赞

2022年7月26日

java实现猜拳游戏

3180阅读·2评论·1点赞

2022年5月4日

JAVA编写猜拳游戏

3037阅读·3评论·3点赞

2021年3月16日

[Java教程]17.实战,趣味猜拳小游戏

8040阅读·2评论·3点赞

2020年6月24日

怎么用java实现人机猜拳?

1959阅读·6评论·9点赞

2021年7月22日

Java Random经典例子【猜拳游戏】

4318阅读·0评论·0点赞

2014年3月22日

java的人机猜拳代码_Java实现人机猜拳游戏

702阅读·0评论·2点赞

2021年3月12日

Java基础练习之猜拳游戏

363阅读·1评论·1点赞

2021年8月19日

用java写猜拳小游戏

1096阅读·0评论·1点赞

2021年9月1日

Java猜拳小游戏

97阅读·0评论·0点赞

2022年8月23日

java猜拳小游戏

500阅读·1评论·0点赞

2022年7月14日

java程序猜拳的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现猜数游戏、java程序猜拳的信息别忘了在本站进行查找喔。