使用java实现猜拳小游戏
作者:YL5335 时间:2023-11-25 17:30:16
本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下
实现下图要求
public class User {
private String u_name;
private int u_score;
public User() {
super();
}
public User(String name, int score) {
super();
this.u_name = name;
this.u_score = score;
}
public String getName() {
return u_name;
}
public void setName(String name) {
this.u_name = name;
}
public int getScore() {
return u_score;
}
public void setScore(int score) {
this.u_score = score;
}
/**
* 出拳方法
* @param choice 选择的数字代表出拳(1:石头2:剪刀3:布)
* @return str 返回你所选择的出拳
*/
public String chuQuan(int choice){
String str = "";
switch (choice) {
case 1:
str = "石头";
break;
case 2:
str = "剪刀";
break;
case 3:
str = "布";
break;
default:
System.out.println("未知错误");
break;
}
return str;
}
}
public class Computer {
private String c_name;
private int c_score;
public String getName() {
return c_name;
}
public void setName(String name) {
this.c_name = name;
}
public int getScore() {
return c_score;
}
public void setScore(int score) {
this.c_score = score;
}
/**
* 出拳方法
* @param choice 选择的数字代表出拳(1:石头2:剪刀3:布)
* @return str 返回你所选择的出拳
*/
public String chuQuan(int choice){
String str = "";
switch (choice) {
case 1:
str = "石头";
break;
case 2:
str = "剪刀";
break;
case 3:
str = "布";
break;
default:
System.out.println("未知错误");
break;
}
return str;
}
}
import java.util.Scanner;
public class Game {
Scanner input = new Scanner(System.in);
private User user;
private Computer computer;
private int count;
private int c_score;
private int u_score;
//初始化方法
public void init(){
user = new User();
computer = new Computer();
System.out.println("-----------------欢迎进入游戏世界------------------");
System.out.println("\t **************************");
System.out.println("\t\t** 猜拳,开始 **");
System.out.println("\t **************************");
System.out.println();
System.out.println("出拳规则:1.石头 2.剪刀 3.布");
System.out.print("请选择对方角色:(1:曹操 2:孙权 3:刘备):");
int key = input.nextInt();
switch (key) {
case 1:
computer.setName("曹操");
break;
case 2:
computer.setName("孙权");
break;
case 3:
computer.setName("刘备");
break;
default:
System.out.println("非法输入...");
break;
}
System.out.print("请输入你的姓名:");
user.setName(input.next());
System.out.println(user.getName()+" VS "+computer.getName());
begin();
}
//是否开始执行 循环执行直到输入n结束
public void begin(){
System.out.print("要开始吗(y/n):");
// boolean falg = true;
String str = input.next();
if(str.equals("y")){
while(true){
score();
System.out.print("是否开始下一轮:(y/n)");
String str1 = input.next();
count++;
if(str1.equals("y")){
}else{
// falg = false;
break;
}
}
}
show();
}
//人和机器出拳并判断胜负 此处计算比赛次数 双方得分
public void score(){
System.out.print("请出拳:");
int choice1 = input.nextInt();
String str1 = user.chuQuan(choice1);
int choice2 = (int)(Math.random()*3+1);
String str2 = computer.chuQuan(choice2);
System.out.println("你出拳"+str1);
System.out.println(computer.getName()+"出拳"+str2);
if(choice1 == choice2){
System.out.println("结果:平局");
}else if(choice2-choice1==-1||choice2-choice1==2){
System.out.println("结果:"+computer.getName()+"获胜...");
c_score++;
computer.setScore(c_score);
}else if(choice1-choice2==-1||choice1-choice2==2){
System.out.println("结果:恭喜你,你获胜...");
u_score++;
user.setScore(u_score);
}
}
//显示比赛结果并比较得得出最后胜负
public void show(){
System.out.println("--------------------------------");
System.out.println(user.getName()+" VS "+computer.getName());
System.out.println("对战次数:"+count+"\n\n");
System.out.println("姓名\t得分");
System.out.println(user.getName()+"\t"+user.getScore());
System.out.println(computer.getName()+"\t"+computer.getScore()+"\n");
if(user.getScore()>computer.getScore()){
System.out.println("结果:恭喜恭喜");
}else if(user.getScore()<computer.getScore()){
System.out.println("结果:再接再厉");
}else{
System.out.println("结果:平局");
}
System.out.println("--------------------------------");
}
}
测试类
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game game = new Game();
game.init();
}
}
这样猜拳小游戏就实现了。
来源:https://blog.csdn.net/YL5335/article/details/119143642
标签:java,猜拳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#之CLR内存原理初探
2023-02-16 00:58:09
![](https://img.aspxhome.com/file/2023/7/102277_0s.png)
c# DataDirectory的用法
2021-07-30 14:36:22
C#中label内容显示不全、不完整的解决方法
2022-09-13 02:43:46
android6.0权限动态申请框架permissiondispatcher的方法
2023-07-31 10:51:57
手动编译C#代码的方法
2021-05-27 05:42:40
![](https://img.aspxhome.com/file/2023/1/91381_0s.png)
Java中的ArrayList容量及扩容方式
2023-10-17 17:24:40
Java进程cpu占用过高问题解决
2021-08-09 00:16:59
![](https://img.aspxhome.com/file/2023/7/70337_0s.jpg)
详解flutter中常用的container layout实例
2022-07-25 07:49:53
![](https://img.aspxhome.com/file/2023/6/113306_0s.png)
基于Viewpager2实现登录注册引导页面
2023-06-16 11:04:10
![](https://img.aspxhome.com/file/2023/5/122055_0s.gif)
Java全面解析string类型的xml字符串
2021-06-03 21:15:18
![](https://img.aspxhome.com/file/2023/7/91727_0s.png)
Android仿微信选择图片和拍照功能
2023-08-18 05:22:50
SpringBoot路径映射实现过程图解
2023-11-13 04:01:11
![](https://img.aspxhome.com/file/2023/6/59376_0s.png)
java利用数组随机抽取幸运观众
2023-11-11 11:14:33
![](https://img.aspxhome.com/file/2023/0/59400_0s.jpg)
基于获取JAVA路径,包括CLASSPATH外的路径的方法详解
2022-12-20 21:30:03
Mybatis利用OGNL表达式处理动态sql的方法教程
2022-11-26 22:22:02
Spring框架的环境搭建和测试实现
2023-10-27 17:57:57
![](https://img.aspxhome.com/file/2023/4/108434_0s.jpg)
eclipse下搭建hibernate5.0环境的步骤(图文)
2022-09-26 02:48:57
![](https://img.aspxhome.com/file/2023/0/83060_0s.jpg)
Json字符串转Java对象和List代码实例
2022-03-11 15:47:09
Android实现中轴旋转特效 Android制作别样的图片浏览器
2023-07-03 00:37:16
![](https://img.aspxhome.com/file/2023/2/104662_0s.png)
C#学习笔记——基本语法
2022-10-13 13:39:54
![](https://img.aspxhome.com/file/2023/7/81107_0s.png)