石头剪刀布的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();

}

}

上一篇:mysql的UseAffectedRows问题 以及其他常见配置说明


下一篇:【Codeforces 682C】Alyona and the Tree