版本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之猜拳游戏的更多相关文章

  1. 有趣的java小项目------猜拳游戏

    package com.aaa; //总结:猜拳游戏主要掌握3个方面:1.人出的动作是从键盘输入的(System.in)2.电脑是随机出的(Random随机数)3.双方都要出(条件判断) import ...

  2. python与java的猜拳游戏

    python版: import randomprint("-----猜拳游戏-----")print("---0.剪刀--1.石头--2.布---")while ...

  3. 人机猜拳游戏Java

    作业要求: 我的代码: package day20181119;/** * 猜拳游戏 * @author Administrator * @version1.0 */import java.util. ...

  4. Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观

    Java 入门课程视频实战-0基础 已经上传完了.欢迎小伙伴们过来围观 直接进入: http://edu.csdn.net/course/detail/196 课程文件夹例如以下: 1 初识Java  ...

  5. 猜拳游戏三局两胜------java实现代码

    package com.javasm.exerices02; import java.util.ArrayList; import java.util.List; import java.util.R ...

  6. Java中利用随机数的猜拳游戏

    Java中利用随机数的猜拳游戏,实现非常简单,重难点在于随机数的产生. 首先GameJude类是用于判断输赢的一个类: package testGame; public class GameJudge ...

  7. java 人机猜拳 游戏

    人机猜拳-游戏 掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用 定义一个电脑类:Computer.java 点击查看[Computer.java]代码 /** * @Title: 电脑类 ...

  8. JAVA 猜拳游戏

    JAVA 猜拳游戏 题目:通过控制台方式实现一个人机对战的猜拳游戏 用户通过输入(0.石头子 1.剪刀 2.布),机器随机生成(0.石头子 1.剪刀 2.布) 要求: 能打印玩家的对局信息,胜利的次数 ...

  9. 用Java编写的猜拳小游戏

    学习目标: 熟练掌握各种循环语句 例题: 代码如下: // 综合案例分析,猜拳案例 // isContinue为是否开始游戏时你所输入的值 char isContinue; //y为开始,n为借宿 S ...

随机推荐

  1. 接口测试命令Httpie的使用

    相比于curl命令,Httpie提供更清晰友好的界面,并支持授权,代理等操作 主要特性 直观的语法 格式化和色彩化的终端输出 内置 JSON 支持 支持上传表单和文件 HTTPS.代理和认证 任意请求 ...

  2. Java基础系列 - 数组、二维数组、对象数组

    package com.test2; public class demo2 { public static void main(String[] args) { /** * 一维数组使用 */ //数 ...

  3. oracle 导入导出功能

    关于expdp和impdp 使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. - EXPDP和IMPDP是服务端的工具程序,他 ...

  4. C#Winform ListView中没有Item双击事件的两种实现方法!

    第一种: //if (this.listView1.FocusedItem != null) //{ // if (this.listView1.SelectedItems != null) // { ...

  5. 快速安装python3

    使用 rpm 包进行安装 先来介绍一下 IUS 这个社区,名字的全写是[Inline with Upstream Stable]取首字母,它主要是一个提供新版本RPM包的社区.具体使用可以查看官方文档 ...

  6. angular 中如何绑定属性

    <!--The content below is only a placeholder and can be replaced.--> <div style="text-a ...

  7. StringBuider类

    特点: 线程不安全的可变字符序列 ; 线程不安全对应的效率高 ; 用法同StringBuffer一致

  8. NetHogs监控Linux的每个进程流量

    在日常运维环境中,我们肯定会遇到以下这种需求: 1.网络流量异常,不知道是哪个程序的流量爆涨? 2.日常需要监控网络实时的流量进去数据 面试装逼系列|这篇文章,让运维监控不再成为你的短板! 学会这 1 ...

  9. [Scikit-learn] 1.1 Generalized Linear Models - from Linear Regression to L1&L2

    Introduction 一.Scikit-learning 广义线性模型 From: http://sklearn.lzjqsdd.com/modules/linear_model.html#ord ...

  10. Apache配置优化之开启KeepAlive

    在HTTP 1.0中和Apache服务器的一次连接只能发出一次HTTP请求,而KeepAlive参数支持HTTP 1.1版本的一次连接,多次传输功能,这样就可以在一次连接中发出多个HTTP请求.从而避 ...