使用java实现猜拳小游戏
作者:YL5335 发布时间:2023-11-25 17:30:16
标签: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();
}
}
这样猜拳小游戏就实现了。
来源:https://blog.csdn.net/YL5335/article/details/119143642
0
投稿
猜你喜欢
- 大家好,这是 [C#.NET 拾遗补漏] 系列的第 07 篇文章。在 C# 中,大多数方法都是通过 return 语句立即把程序的控制权交回
- 最近一段时间,大家在用 Spring Security OAuth2 时可能发现有很多类过期了。大家在选择 OAuth2 依赖的时候,可能也
- 通过shift+shift可以调出搜索窗口或者ctrl+n但是,如果想搜索jdk中的类,只是在搜索栏中是无法搜出来的需要勾选 红框内的选项没
- 注:由于工作需要, 也是第一次接触到打印机的相关内容, 凑巧, 通过找了很多资料和帮助后, 也顺利的解决了打印标签的问题(标签的表面信息[二
- 添加MyBatis的代码并修改以下部分:1.添加MyBatisConfigpackage myshop.config;import java
- 本文实例为大家分享了java导出百万以上数据的excel文件,供大家参考,具体内容如下1.传统的导出方式会消耗大量的内存,2003每个she
- 在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,很多如系统配置信息,文件上传配置信息等等都是以这种方式进行
- 一、JDK中常见的异常情况1、常见异常总结图2、java中异常分类Throwable类有两个直接子类:(1)Exception:出现的问题是
- 简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特
- 异常的练习:老师用电脑上课。开始思考上课中出现的问题。比如问题是电脑蓝屏。电脑冒烟。要对问题进行描述,封装成对象。可是当冒烟发生后,出现讲课
- 一 关键pom<dependencies> <dependency> <groupId>or
- 介绍本文实现的功能有:1、播放音乐2、自定义流星数量、飞行速度、光晕大小、流星大小3、自定义表白话语 运用到的知识点有:GUI:j
- SEATA概要seata 是alibaba 出的一款分布式事务管理器,他有侵入性小,实现简单等特点。我们能够使用seata 实现分布式事务管
- 一直以来做对外的接口文档都比较原始,基本上都是手写的文档传来传去,最近发现了一个新玩具,可以在接口上省去不少麻烦。swagger是一款方便展
- 本文实例讲述了JAVA设计模式之备忘录模式。分享给大家供大家参考,具体如下:备忘录模式:又叫做快照模式,指在不破坏封装性的前提下,获取到一个
- 阿里终面在线编程题,写出来与大家分享一下 有一个单向链表
- Maven工程分模块开发完成 父工程配置了 tomcat7插件,运行 run命令执行run指令时失败,报错信息如下:D:\java\JDK8
- 确保这个修改是正确的(否则将会出现乱码)创建i18n文件夹(就是国际化的意思),然后在此文件加下创login.properties logi
- 一、简介地图控件自v2.3.5版本起,支持多实例,即开发者可以在一个页面中建立多个地图对象,并且针对这些对象分别操作且不会产生相互干扰。文件
- 1、this关键字首先需要提醒的是,在整个Java之中,this是最麻烦的一个关键字,只要是代码开发,几乎都离不开this。在Java中th