人机猜拳游戏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 角色登陆: ******************** ***欢迎进入猜拳游戏*** **************** ...
随机推荐
- python threading
//test.py 1 import threading 2 import time 3 4 exitFlag = 0 5 6 class myThread (threading.Thread): 7 ...
- 输出调试技巧 PRINTF()
#define PRINTF(...) \ do { \ printf( "%d:%s::",__LINE__, __FUNCTION__);\ printf(__VA_ARGS_ ...
- MyBatis基础入门《十五》ResultMap子元素(collection)
MyBatis基础入门<十五>ResultMap子元素(collection) 描述: 见<MyBatis基础入门<十四>ResultMap子元素(association ...
- c# 调试模式下Swaggerf附加接口参数
c# 调试模式下Swaggerf附加接口参数,如:每个接口Header中附加参数appId 1.新增过滤器: public class GlobalHttpHeaderFilter : IOperat ...
- tp视图模板
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { ...
- [9]Windows内核情景分析 --- DPC
DPC不同APC,DPC的全名是'延迟过程调用'. DPC最初作用是设计为中断服务程序的一部分.因为每次触发中断,都会关中断,然后执行中断服务例程.由于关中断了,所以中断服务例程必须短小精悍,不能消耗 ...
- SlimScroll插件学习
SlimScroll插件学习 SlimScroll插件,是一个很好用的滚动条插件. 第一个实例程序: js代码: <script src="../slimScroll/jquery-3 ...
- Yii ActiveRecord生命周期
- poj2114 寻找树上存在长度为k点对,树上的分治
寻找树上存在长度为k点对,树上的分治 代码和 这个 差不多 ,改一下判断的就好 #include <iostream> #include <algorithm> #inc ...
- D Tree Requests dfs+二分 D Pig and Palindromes -dp
D time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan ...