石头剪刀布的JAVA小程序 供初学者参考
Posted 南岭寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了石头剪刀布的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();
}
}
以上是关于石头剪刀布的JAVA小程序 供初学者参考的主要内容,如果未能解决你的问题,请参考以下文章