人机猜拳游戏Java
作业要求:








我的代码:
package day20181119;
/**
* 猜拳游戏
* @author Administrator
* @version1.0
*/
import java.util.Scanner;
public class FingerGuessing {
String chName;
String name;
int i;
int ch1;
int Score1;
int ch2;
int Score2;
public void showLoginMenu(){
System.out.println("--------------欢迎来到游戏世界------------");
System.out.println("\t***********************");
System.out.println("\t *****猜拳,开始*****");
System.out.println("\t***********************");
System.out.println("\n出拳规则:1.剪刀2.石头3.布");
System.out.print("请选择对方角色:(1.刘备2.孙权3.曹操)");
Scanner input=new Scanner (System.in);
int ch=input.nextInt();
if(ch==1){
chName="刘备";
showStartGame();
}else if(ch==2){
chName="孙权";
showStartGame();
}else if(ch==3){
chName="曹操";
showStartGame();
}else{
System.out.println("您的输入有误,请重新输入!");
showLoginMenu();
}
}
public void showStartGame(){
Scanner input=new Scanner(System.in);
System.out.print("请输入您的姓名:");
name=input.nextLine();
System.out.println(name+"VS"+chName+"对战!");
showStart();
}
public void showStart(){
Scanner input=new Scanner(System.in);
i=i+1;
System.out.print("\n要开始第"+i+"轮吗?(Y/N)");
String ch=input.nextLine();
if(ch.equals("Y")){
showFist();
}else{
showResult2();
}
}
public void showFist(){
System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");
Scanner input=new Scanner(System.in);
ch1=input.nextInt();
if(ch1==1){
System.out.println("你出拳:剪刀");
showComputer();
}else if(ch1==2){
System.out.println("你出拳:石头");
showComputer();
}else if(ch1==3){
System.out.println("你出拳:布");
showComputer();
}
}
public void showComputer(){
ch2=(int)(Math.random()*3);
if(ch2==0){
System.out.println(chName+"出拳:剪刀");
showResult();
}else if(ch2==1){
System.out.println(chName+"出拳:石头");
showResult();
}else if(ch2==2){
System.out.println(chName+"出拳:布");
showResult();
}
}
public void showResult(){
if(ch1==1&&ch2==1||ch1==2&&ch2==2||ch1==3&&ch2==0){
System.out.println("结果是:^_^,你输了,下次加油啊!");
Score2++;
showStart();
}else if(ch1==1&&ch2==2||ch1==2&&ch2==0||ch1==3&&ch2==1){
System.out.println("结果是:恭喜,你赢了!");
Score1++;
showStart();
}else if(ch1==1&&ch2==0||ch1==2&&ch2==1||ch1==3&&ch2==2){
System.out.println("结果是:和局,真帅!");
showStart();
}
}
public void showResult2(){
System.out.println("------------------------------------");
System.out.println(name+"VS"+chName);
System.out.println("对战次数:"+(i-1));
System.out.println("姓名\t得分");
System.out.println(name+"\t"+Score1);
System.out.println(chName+"\t"+Score2);
if(Score1>Score2){
System.out.print("结果:恭喜恭喜!");
}else if(Score1<Score2){
System.out.println("结果:下次再加油哦!");
}else if(Score1==Score2){
System.out.println("真是皆大欢喜的结局呢!");
}
System.out.println("------------------------------------");
}
}
-------------------------------------------------------------------------------------
package day20181119;
public class TestFingerGuessing {
public static void main(String[] args) {
FingerGuessing game=new FingerGuessing();
game.showLoginMenu();
}
}
人机猜拳游戏Java的更多相关文章
- java 人机猜拳 游戏
人机猜拳-游戏 掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用 定义一个电脑类:Computer.java 点击查看[Computer.java]代码 /** * @Title: 电脑类 ...
- Java 实现简单的人机猜拳游戏
import java.util.Scanner; import java.util.Random; public class TestGuess{ public static void main(S ...
- JAVA 猜拳游戏
JAVA 猜拳游戏 题目:通过控制台方式实现一个人机对战的猜拳游戏 用户通过输入(0.石头子 1.剪刀 2.布),机器随机生成(0.石头子 1.剪刀 2.布) 要求: 能打印玩家的对局信息,胜利的次数 ...
- python与java的猜拳游戏
python版: import randomprint("-----猜拳游戏-----")print("---0.剪刀--1.石头--2.布---")while ...
- 有趣的java小项目------猜拳游戏
package com.aaa; //总结:猜拳游戏主要掌握3个方面:1.人出的动作是从键盘输入的(System.in)2.电脑是随机出的(Random随机数)3.双方都要出(条件判断) import ...
- 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基础代码分享--模拟人机猜拳系统
问题描述: 一.主要功能: .电脑与人互动,实现“剪刀.石头.布”的游戏: 1.1 角色登陆: ******************** ***欢迎进入猜拳游戏*** **************** ...
随机推荐
- SEO--提高权重
搜索引擎提交入口:将新网站提交收录 SEO优化三要素:标题 关键词 描述 外链(友情链接) 目的:提高权重 注意事项: 1.和内容相近的网站交换 2.链接交换形式多样 单向链接:别人指向我或我指向别人 ...
- 通过Response下载。
之前一直在找js下载的插件,后来发现下载必须通过java后台处理,什么write(),open(),close()之类的方法.如果直接是通过<a>标签倒是简单的,直接将路径摆放在上面就可以 ...
- Bootstrap-媒体查询-屏幕大小
.container{padding:0 15px; margin:0 auto;} .container:before{ content: ''; display: table;/*防止第一个子元素 ...
- jdbc连接oracle的几种格式
1. SID的方式.已经不推荐使用这种方式了. jdbc:oracle:thin:[<user>/<password>]@<host>[:<port>] ...
- 002-golang安装配置
1.安装位置: 2.环境变量. path的值如下: 3.工作目录.
- C++中位运算
简介 1 位逻辑运算符: & (位 “与”) and ----------------- 2个都为1 才是1-----------0^0 = 0 , 0^1 = 0, 1^0 = 0 ...
- 5.JVM的内存区域划分
一.JVM介绍 1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟 ...
- 32网络通信之Poll模型
多路复用并发模型 -- poll #include<poll.h> int poll(struct pollfd *fds, unsigned int nfds, int timeo ...
- 编写python的程序
执行python程序有两种方式: 1.交互式环境:输入代码立即执行 优点:调试程序方便 缺点:无法永久保存程序 2.代码写入文件 ...
- html5-css列表和表格
td{ /*width: 150px; height: 60px;*/ padding: 10px; text-align: center;} table{ width ...