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();
    }

    }

  • 相关阅读:
    【转帖】android线程知识整理
    Andorid开发笔记
    Flex 4.6 手机项目第一次开发小记
    memory point
    两个sql server 2000的通用分页存储过程
    网页通用视频播放(asp.net2.0原作)
    c#操作XML常用方法
    也说项目开发经验
    SQL Server各种日期计算方法
    Server.Transfer()方法在页间传值
  • 原文地址:https://www.cnblogs.com/SUN99bk/p/9988240.html
Copyright © 2011-2022 走看看