zoukankan      html  css  js  c++  java
  • 石头剪刀布的JAVA小程序 供初学者参考

    package youxi;

    public class Player {
    private String name;
    private double score;
    public Player(String name, double score) {
    super();
    this.name = name;
    this.score = score;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public double getScore() {
    return score;
    }
    public void setScore(double score) {
    this.score = score;
    }


    private String setScore() {
    // TODO Auto-generated method stub
    return this.setScore() ;
    }


    private String setName() {
    // TODO Auto-generated method stub
    return this.setName();
    }
    }

    package youxi;

    import java.util.Scanner;


    public class Game {

    private Player player;
    public Game (Player player){
    this.player=player;
    }


    public void start(){
    while(true){
    System.out.println("开始游戏");
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入剪刀石头布or退出");
    String s1=sc.next();
    if( s1.equals("退出")){
    break;
    }
    String[] arr=new String[]{"剪刀","石头","布"};
    int count=(int)(Math.random()*4);
    String s2=arr[count];
    int flag=rule(s1,s2);
    if(flag>0){
    System.out.println("机器人"+arr[count]+"~~~~~"+player.getName()+"赢了,加十分");
    player.setScore(player.getScore()+10);

    }
    if(flag<0){
    System.out.println("机器人"+arr[count]+"~~~~~"+player.getName()+"输了,减十分");
    player.setScore(player.getScore()-10);

    }
    else if(flag==0){
    System.out.println("机器人"+arr[count]+"~~~~~"+"平手");
    }
    System.out.println( player.getName()+":"+player.getScore());
    }

    }

    /**
    * 游戏规则
    * @return
    */

    public int rule(String s1,String s2){
    if(s1.equals(s2))
    {return 0;}
    if(s1.equals("剪刀")){
    if(s2.equals("石头")){
    return -1;

    }
    if(s2.equals("布")){
    return 1;
    }
    }
    if(s1.equals("石头")){
    if(s2.equals("布")){
    return -1;

    }
    if(s2.equals("剪刀")){
    return 1;
    }
    }
    if(s1.equals("布")){
    if(s2.equals("剪刀")){
    return -1;

    }
    if(s2.equals("石头")){
    return 1;
    }
    }


    return 0;
    }
    }

    package youxi;

    public class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Player p=new Player("志伟",100);
    Game g=new Game(p);
    g.start();

    }

    }

  • 相关阅读:
    linux下启动和关闭网卡命令及DHCP上网
    python 编码问题
    paddlepaddle
    Convolutional Neural Network Architectures for Matching Natural Language Sentences
    deep learning RNN
    Learning Structured Representation for Text Classification via Reinforcement Learning 学习笔记
    Python IO密集型任务、计算密集型任务,以及多线程、多进程
    EM 算法最好的解释
    tensorflow 调参过程
    tensorflow 学习纪录(持续更新)
  • 原文地址:https://www.cnblogs.com/nanlinghan/p/7749284.html
Copyright © 2011-2022 走看看