码迷,mamicode.com
首页 > 编程语言 > 详细

石头剪刀布的JAVA小程序 供初学者参考

时间:2017-10-29 11:08:32      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:开始游戏   ext   generated   nbsp   public   石头   []   ann   小程序   

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

}

}

石头剪刀布的JAVA小程序 供初学者参考

标签:开始游戏   ext   generated   nbsp   public   石头   []   ann   小程序   

原文地址:http://www.cnblogs.com/nanlinghan/p/7749284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!