「java实现猜拳游戏」java猜拳游戏窗体版
本篇文章给大家谈谈java实现猜拳游戏,以及java猜拳游戏窗体版对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、猜拳游戏java能输出游戏时间
- 2、应该怎么写这个JAVA猜拳游戏
- 3、用JAVA做一个剪刀,石头,布的人机猜拳游戏。
- 4、java猜拳游戏 (3局2胜)
- 5、求一个java猜拳游戏程序
- 6、用java如何编写一个猜拳游戏?
猜拳游戏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做一个剪刀,石头,布的人机猜拳游戏。
编写这个小游戏 我们需要几个类
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猜拳游戏 (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如何编写一个猜拳游戏?
我之前写了个猜拳游戏的源代码,不过没你想的这么精彩。你才给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实现猜拳游戏的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java猜拳游戏窗体版、java实现猜拳游戏的信息别忘了在本站进行查找喔。