欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单猜拳小游戏

程序员文章站 2022-06-18 15:41:24
本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下user.javaimport java.util.scanner;public class user { stri...

本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下

user.java

import java.util.scanner;
public class user {
    string name;
    int score;
    public int showfist(){
        system.out.println ("请出拳:1.剪刀\t2.石头\t3.布");
        scanner input=new scanner ( system.in );
        int choice=input.nextint ();
        if(choice==1){
            system.out.println ("您出了剪刀");
        }else if(choice==2){
            system.out.println ("您出了石头");
        }else if (choice==3){
            system.out.println ("您出了布");
        }else {
            system.out.println ("输入有误!");
        }
        return choice;
    }
}

computer.java

public class computer {
    string name;
    int score;

    public int showfist () {
        int choice = (int) (math.random () * 3) + 1;    //产生随机数
        if (choice == 1) {
            system.out.println (name +"出了剪刀" );
        } else if (choice == 2) {
            system.out.println (name +"出了石头" );
        } else if (choice == 3) {
            system.out.println (name +"您出了布" );
        } else {
            system.out.println ( "输入有误!" );
        }
        return choice;
    }
}

game.java

import java.util.scanner;

public class game {
    user user;  //用户
    computer computer;  //计算机
    int count;  //记录对战次数

    //初始化:设置自己的名字   对手的名字,积分0
    public void init(){
        system.out.println ("请输入自己的名字");
        scanner input = new scanner ( system.in );
        string name = input.next ();
        user=new user ();   //对象初始化
        user.name=name;
        user.score=0;
        system.out.println ("请选择你的对手:\n1.张三\t2.李四\t3.王五");
        int choice = input.nextint ();
        computer=new computer ();   //对象初始化
        computer.score=0;
        switch (choice){
            case 1:
                computer.name="张三";
            case 2:
                computer.name="李四";
            case 3:
                computer.name="王五";
                break;
            default:
                system.out.println ("输入有误!");
        }
        system.out.println ("你选择与ta对战:"+computer.name);
    }

    public void start() {
        init ();
        scanner input=new scanner ( system.in );
        string iscontinue = null;
        do {
            int userfist = user.showfist ();              //人出拳
            int chomputerfist = computer.showfist ();     //计算机出拳
            calcresult ( userfist, chomputerfist );
            system.out.println ("是否继续?y(继续)/其他(结束)");
            iscontinue=input.next ();
        }while ("y".equals ( iscontinue ));
         showresult (user,computer);    //显示最终结果
    }
    //计算每一轮的结果
    public void calcresult(int userfist,int computerfist){
        //"1.剪刀\t2.石头\t3.布"
        if((userfist==1&&computerfist==3)||(userfist==2&&computerfist==1)||(userfist==3&&computerfist==2)){
            system.out.println ("您赢了");
            user.score++;
        }else if((userfist==3&&computerfist==1)||(userfist==1&&computerfist==2)||(userfist==2&&computerfist==3)){
            system.out.println ("您输了");
            computer.score++;
        }else {
            system.out.println ("您平局");
        }


    }
    //计算最终结果
    public void showresult(user user,computer computer){
        system.out.println (user.name +"\t" +user.score );
        system.out.println (computer.name +"\t" +computer.score);
        if (user.score>computer.score){
            system.out.println ("恭喜,获得了最终的胜利");
        }else if (user.score<computer.score){
            system.out.println ("很遗憾你输了");
        }else {
            system.out.println ("最终平局...");
        }

    }

    public static void main ( string[] args ) {
        game game = new game ();
        game.start ();
    }
}

测试结果显示

java实现简单猜拳小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: java 猜拳