zoukankan      html  css  js  c++  java
  • 人机猜拳游戏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(" ***********************");
    System.out.println(" *****猜拳,开始*****");
    System.out.println(" ***********************");
    System.out.println(" 出拳规则: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(" 要开始第"+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("姓名 得分");
    System.out.println(name+" "+Score1);
    System.out.println(chName+" "+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();
    }

    }

  • 相关阅读:
    C/C++字符串函数之复制函数
    tesseract api C++使用例子
    error C2275: “XXX”: 将此类型用作表达式非法
    Socket通信原理探讨(C++为例)
    模拟按键,点击,滑动,在光标处输出字符
    安卓使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)
    【 转】__try,__except,__finally,__leave异常模型机制
    提高VS2010运行速度的技巧
    解决VS2010子目录中的.cpp文件引用上一级目录的stdafx.h找不到定义的问题
    1009MySQL数据库InnoDB存储引擎Log漫游
  • 原文地址:https://www.cnblogs.com/SUN99bk/p/9988240.html
Copyright © 2011-2022 走看看