网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java人机猜拳代码 人机猜拳代码python

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

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

我们提供的服务有:成都网站制作、网站设计、微信公众号开发、网站优化、网站认证、察哈尔右翼前ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的察哈尔右翼前网站制作公司

1、第一个 Person 类

import java.util.Scanner;

/**

* @copyright 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @ClassName Person

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

*/

public class Person {

public static final Person me = new Person();

private int n = 0;

/**

* @copyright 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @description 用户输入的 指令

* @return

*/

public int input() {

System.out.println("请输入:石头,剪刀,布\n输入:@退出 退出系统");

@SuppressWarnings("resource")

Scanner scanner = new Scanner(System.in);

String s = scanner.next();

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

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

n = 1;

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

n = 2;

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

n = 3;

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

System.out.print("系统退出了");

System.exit(0);

}

return n;

}

}

2、Computer 类

/**

* @copyright 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @ClassName Computer

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

*/

public class Computer {

public static final Computer me = new Computer();

/**

* @copyright 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @description TODO

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

*/

public int random() {return (int) (Math.random() * 3 + 1);}

}

3、Game类

/**

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @ClassName Game

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

*/

public class Game {

/**

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @description 返回 人机交互结果

* @param n

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

* @param m

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

*/

public void result(int n, Integer m) {

String res = "";

if (m.intValue() == 1)

res = "石头";

else if (m.intValue() == 2)

res = "剪刀";

else

res = "布";

if (n == m) {

System.out.println("平了 computer出" + res);

} else {

if (m == 1) {

if (n == 2)

System.out.println("你输了 computer出 " + res);

else if (n == 3)

System.out.println("你赢了 computer出 " + res);

} else if (m == 2) {

if (n == 1)

System.out.println("你赢了 computer出 " + res);

else if (n == 3)

System.out.println("你输了 computer出 " + res);

} else if (m == 3) {

if (n == 1)

System.out.println("你输了 computer出 " + res);

else if (n == 2)

System.out.println("你赢了 computer出 " + res);

}

}

}

public static void main(String[] args) {

while (true) {

Game gamer = new Game();

gamer.result(Person.me.input(), Computer.me.random());

}

}

}

如下是程序运行截图

猜拳游戏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人机猜拳的代码,类似一下界面。

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

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编写人机猜拳游戏

1. String[] j={"角色1","角色2","角色3","角色4","角色5"};//创建和存储5个角色

2. new Random().nextInt(5); //产生一个0-5的随机数

3.积分数据可以存放在数据库中,也可以存放在本地, SESSION,等等..

java ;while()循环。好难啊!

难什么呢

很简单啊

你设置一个随机数 做机器

int n = 1+(int)(Math.random()*3); 随机参数1,2,3 三个数

跟用户输入的数字进行比较 如果大于 则机器人赢了

相等 则平手

是否根据用户输入 退出 System.exit(0);

求java人机猜拳游戏的程序

我之前写了个猜拳游戏的源代码,不过没你想的这么精彩。你才给5分就给你你自己修改了,应该很简单的。要多给点分我可以帮你修改。

import java.util.Scanner;

import java.util.Random;

public class caiquan

{

final int jiandao=0;

final int shitou=1;

final int bu=2;

public static void main(String[] args)

{

String yn=;y;;

while (yn.equals(;y;))

{

Scanner scanner = new Scanner(System.in);

System.out.println(;欢迎玩猜拳游戏。请输入0,1,2:0表示剪刀,1表示石头,2表示布;);

int a = scanner.nextInt();

Random rd = new Random();

int b = rd.nextInt(3);

switch (b)

{

case 0:

{

System.out.println(;系统出的是剪刀;);

switch(a)

{

case 0:System.out.println(;平;);break;

case 1:System.out.println(;赢;);break;

case 2:System.out.println(;输;);break;

}

}

break;

case 1:

{

System.out.println(;系统出的是石头;);

switch(a)

{

case 0:System.out.println(;输;);break;

case 1:System.out.println(;平;);break;

case 2:System.out.println(;赢;);break;

}

}

break;

case 2:

{

System.out.println(;系统出的是布;);

switch(a)

{

case 0:System.out.println(;赢;);break;

case 1:System.out.println(;输;);break;

case 2:System.out.println(;平;);break;

}

}

}

Scanner ynn = new Scanner(System.in);

System.out.println(;是否继续?是请输入y,否则输入n。;);

yn=ynn.next();

}

}

}


分享文章:java人机猜拳代码 人机猜拳代码python
新闻来源:http://bjjierui.cn/article/dodsiid.html

其他资讯