java--demo之猜拳游戏
版本1:人机大战 基础随机出 用户键盘录入
package com.hainiu.demo; import java.util.Scanner; /*
* 人机大战石头剪刀布
*/
public class Cycles { public static void main(String[] args) {
while(true){
System.out.println("-----欢迎来到游戏界面----");
System.out.println("1:剪刀、2:石头、3:布");
Scanner sc = new Scanner(System.in);
int person = sc.nextInt();
int computer = (int)(Math.random()*(3)+1);
String per="用户";
String com="电脑";
//用户
switch(person){
case 1:
per="剪刀";
break;
case 2:
per="石头";
break;
case 3:
per="布";
break;
//电脑
}
switch(computer){
case 1:
com="剪刀";
break;
case 2:
com="石头";
break;
case 3:
com="布";
break;
//判断
}
if(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("你输啦");
}else if(person==2&&computer==1||person==3&&computer==2||person==1&&computer==3){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("你赢了");
}else if(person==computer){
System.out.println("你出的是"+per+"电脑出的是"+com);
System.out.println("平局");
}else{
System.out.println("您的输入有误");
break;
}
}
}
}
运行结果:
-----欢迎来到游戏界面----
1:剪刀、2:石头、3:布
2
你出的是: 石头 电脑出的是: 剪刀
你赢了
-----欢迎来到游戏界面----
1:剪刀、2:石头、3:布
版本2:
猜拳游戏说明:
⦁ 任务
⦁ 完成人机猜拳互动游戏的开发
⦁ 主要功能
⦁ 选取对战角色
⦁ 猜拳
⦁ 记录分数
⦁ 需求说明
⦁ 分析业务
⦁ 抽象出类、类的特征和行为
⦁ 实现思路:
⦁ 分析业务,抽象出类、类的特征和行为
package com.hainiu.demo;
import java.util.Scanner;
class User{
Scanner sc = new Scanner(System.in);
private String name;
private int integral;
private String punch;
public User() {
// TODO Auto-generated constructor stub
}
public User(String name, int integral, String punch) {
super();
this.name = name;
this.integral = integral;
this.punch = punch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPunch() {
return punch;
}
public void setPunch(String punch) {
this.punch = punch;
}
//用户输入名称
public void inputName(){
System.out.println("请输入用户录入名");
name = sc.next();
}
//猜拳方法
public void UserGuess(){
int n= sc.nextInt();
System.out.println("1、剪刀,2、石头、3、布");
if(n>0&&n<=3){
switch (n) {
case 1:
this.punch="剪刀";
break;
case 2:
this.punch="石头";
break;
case 3:
this.punch="布";
break;
}
}else{
System.out.println("输入有误");
}
}
}
class Computer{
Scanner sc = new Scanner(System.in);
private String name;
private int integral;
private String punch;
public Computer() {
}
public Computer(Scanner sc, String name, int integral, String punch) {
this.sc = sc;
this.name = name;
this.integral = integral;
this.punch = punch;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public String getPunch() {
return punch;
}
public void setPunch(String punch) {
this.punch = punch;
}
//选择用户
public void computerName(){
System.out.println("请选择您的对手:");
System.out.println("1、扫地僧,2、逍遥子、3、独孤求败");
int n = sc.nextInt();
switch (n) {
case 1:
this.name="扫地僧";
break;
case 2:
this.name="逍遥子";
break;
case 3:
this.name="独孤求败";
break;
}
}
//猜拳方法
public void comuterGuess(){
//System.out.println("对手出的是:");
int t = (int)(Math.random()*(3)+1);
switch (t) {
case 1:
this.punch="剪刀";
break;
case 2:
this.punch="石头";
break;
case 3:
this.punch="布";
break;
}
}
}
class TestGame{
User u = new User();
Computer c =new Computer();
//欢迎进入游戏菜单
public void start(){
//初始化对象
System.out.println("欢迎进入游戏菜单");
System.out.println("游戏开始");
//选择您的对手
c.computerName();
u.inputName();
System.out.println(u.getName()+"vs"+c.getName());
}
//判断出拳的结果
public void game(){
u.UserGuess();;
c.comuterGuess();
System.out.println("你出的是"+u.getPunch());
System.out.println(c.getName()+"出的是"+c.getPunch());
//具体判断输赢的 剪刀 石头 布
if(u.getPunch().equals("剪刀") && c.getPunch().equals("石头")||u.getPunch().equals("石头") && c.getPunch().equals("布") || u.getPunch().equals("布") && c.getPunch().equals("剪刀")){
c.setIntegral(c.getIntegral()+1);
System.out.println("你输了");
}else if(u.getPunch().equals("石头")&&c.getPunch().equals("剪刀")||u.getPunch().equals("布")&&c.getPunch().equals("石头")||u.getPunch().equals("剪刀")&&c.getPunch().equals("布")){
u.setIntegral(u.getIntegral()+1);
System.out.println("你赢啦");
}else {
System.out.println("平局");
}
}
public void last(){
System.out.println("最后结果是:");
System.out.println(u.getName()+"赢了"+u.getIntegral()+"局");
System.out.println(c.getName()+"赢了"+c.getIntegral()+"局");
if(u.getIntegral()>c.getIntegral()){
System.out.println("算总分"+u.getName()+"或的最后的胜利");
}else if(u.getIntegral()<c.getIntegral()){
System.out.println("算总分"+c.getName()+"或的最后的胜利");
}else if(u.getIntegral()==c.getIntegral()){
System.out.println("最后平局");
}else {
System.out.println("输入有误,这不是系统的问题");
}
}
}
public class Cycles2 {
public static void main(String[] args) {
// 测试
Scanner sc = new Scanner(System.in);
System.out.println("---------游戏开始----------\n");
TestGame tg = new TestGame();
tg.start();
System.out.println("输入y继续,其他人任意键结束");
String panduan = sc.next();
//循环game方法
while(panduan.equals("y")){
System.out.println("******************");
System.out.println("1、剪刀,2、石头、3、布");
tg.game();
System.out.println("y:继续-任意:结束");
panduan = sc.next();
}
System.out.println("游戏结束");
tg.last();
}
}
java--demo之猜拳游戏的更多相关文章
- 有趣的java小项目------猜拳游戏
package com.aaa; //总结:猜拳游戏主要掌握3个方面:1.人出的动作是从键盘输入的(System.in)2.电脑是随机出的(Random随机数)3.双方都要出(条件判断) import ...
- python与java的猜拳游戏
python版: import randomprint("-----猜拳游戏-----")print("---0.剪刀--1.石头--2.布---")while ...
- 人机猜拳游戏Java
作业要求: 我的代码: package day20181119;/** * 猜拳游戏 * @author Administrator * @version1.0 */import java.util. ...
- Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观
Java 入门课程视频实战-0基础 已经上传完了.欢迎小伙伴们过来围观 直接进入: http://edu.csdn.net/course/detail/196 课程文件夹例如以下: 1 初识Java ...
- 猜拳游戏三局两胜------java实现代码
package com.javasm.exerices02; import java.util.ArrayList; import java.util.List; import java.util.R ...
- Java中利用随机数的猜拳游戏
Java中利用随机数的猜拳游戏,实现非常简单,重难点在于随机数的产生. 首先GameJude类是用于判断输赢的一个类: package testGame; public class GameJudge ...
- java 人机猜拳 游戏
人机猜拳-游戏 掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用 定义一个电脑类:Computer.java 点击查看[Computer.java]代码 /** * @Title: 电脑类 ...
- JAVA 猜拳游戏
JAVA 猜拳游戏 题目:通过控制台方式实现一个人机对战的猜拳游戏 用户通过输入(0.石头子 1.剪刀 2.布),机器随机生成(0.石头子 1.剪刀 2.布) 要求: 能打印玩家的对局信息,胜利的次数 ...
- 用Java编写的猜拳小游戏
学习目标: 熟练掌握各种循环语句 例题: 代码如下: // 综合案例分析,猜拳案例 // isContinue为是否开始游戏时你所输入的值 char isContinue; //y为开始,n为借宿 S ...
随机推荐
- 【原创】sed正则表达式替换
1.数字替换原数字 sed -i "s/\([0-9]*\)/\1/g"
- ROS计算图级
上一节说到一个 package 可以包含多个可执行文件(节点),可执行文件需要被运行,就要了解ROS的通信架构,也就是计算图级,例: 小萝卜机器人拥有驱动系统,感知系统,控制系统等,要让它从指定位置到 ...
- php curl 传递数据
<?php header("Content-type: text/html; charset=utf-8"); /** * curl 传递数据 */ class curl { ...
- [bzoj 4887] [Tjoi2017]可乐
传送门 Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市 ...
- 怎么看部分元素的js代码?
- [转]Python3之max key参数学习记录
Python3之max key参数学习记录 转自https://www.cnblogs.com/zhangwei22/p/9892422.html 今天用Python写脚本,想要实现这样的功能:对于给 ...
- Mininet系列实验(四):基于Mininet测量路径的损耗率
1 实验目的 熟悉Mininet自定义拓扑脚本的编写与损耗率的设定: 熟悉编写POX脚本,测量路径损耗速率 2 实验原理 在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为,此外 ...
- Django 测试开发3 数据模型models和admin管理工具
参考:https://blog.csdn.net/weixin_44510615/article/details/89425412 1.Django模型字段常用类型: IntegerField : 整 ...
- linux内核是如何支持深度睡眠(deep sleep)方式的?
1. 硬件架构 arm64 2. 内核版本 4.19 3. 分析相关函数 setup_arch() -> psci_dt_init() -> psci_0_2_init() -> g ...
- SQL-W3School-函数:SQL FORMAT() 函数
ylbtech-SQL-W3School-函数:SQL FORMAT() 函数 1.返回顶部 1. FORMAT() 函数 FORMAT 函数用于对字段的显示进行格式化. SQL FORMAT() 语 ...