使用java实现猜拳小游戏
更新时间:2021年07月30日 14:12:30 作者:YL5335
这篇文章主要为大家详细介绍了使用java实现猜拳小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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();
}
}
这样猜拳小游戏就实现了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
SpringBoot日志配置SLF4J和Logback的方法实现
日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2025-04-04
java通过Idea远程一键部署springboot到Docker详解
这篇文章主要介绍了java通过Idea远程一键部署springboot到Docker详解,Idea是Java开发利器,springboot是Java生态中最流行的微服务框架,docker是时下最火的容器技术,那么它们结合在一起会产生什么化学反应呢?的相关资料2019-06-06
SpringCloud Config分布式配置中心使用教程介绍
springcloud config是一个解决分布式系统的配置管理方案。它包含了 client和server两个部分,server端提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client端通过接口获取数据、并依据此数据初始化自己的应用2022-12-12
浅谈Java中ThreadLocal内存泄露的原因及处理方式
内存泄漏就是我们申请了内存,但是该内存一直无法释放,就会导致内存溢出问题,本文详细的介绍了ThreadLocal内存泄露的原因及处理方式,感兴趣的可以了解一下2023-05-05


最新评论