标签:bst util bsp nbsp logs 比较 system player .sh
package ppt10lang包.猜拳游戏; public abstract class Player { private String name;//用户名 private int score;//积分 public Player() { } public Player(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } /** * 出拳 */ public abstract int showFist(); }
package ppt10lang包.猜拳游戏; import java.util.Scanner; public class Person extends Player{ public Person(String pname){ super(pname); } @Override public int showFist() { System.out.println("请用户输入1石头 2剪刀 3布"); Scanner input = new Scanner(System.in); int fist = input.nextInt(); return fist; } }
package ppt10lang包.猜拳游戏; import java.util.Random; /** * 电脑出拳 * @author Administrator * */ public class Computer extends Player{ public Computer(String cname){ super(cname); } @Override public int showFist() { //随机数 1-3 Random random = new Random(); int fist = random.nextInt(3) +1; return fist; } }
package ppt10lang包.猜拳游戏; public class Game { private Person p; private Computer c; // private int i = 1; public Game(Person p,Computer c){ this.p = p; this.c = c; } /** * pk * 判断当前局的输赢结果 */ public void pk(){ //提升部分:五局 for (int i = 1; i <= 5; i++) { System.out.println("第" + i +"局"); //获取玩家出拳的数字 int pfist = p.showFist(); //获取电脑出拳的数字 int cfist = c.showFist(); //获取玩家出拳的具体 String pValue = getFistValue(pfist); //获取电脑出拳的具体 String cValue = getFistValue(cfist); //比较规则 if ((pfist == 1 && cfist == 2) ||(pfist == 2 && cfist == 3) || (pfist == 3 && cfist == 1) ) { System.out.println(p.getName() + "玩家出拳:" + pValue); System.out.println(c.getName() + "电脑出拳:" + cValue); System.out.println("恭喜你:" + p.getName() ); //用户积分 +1 p.setScore( p.getScore() + 1 ); }else if (pfist == cfist) { System.out.println(p.getName() + "玩家出拳:" + pValue); System.out.println(c.getName() + "电脑出拳:" + cValue); System.out.println("平局" ); }else{ System.out.println(p.getName() + "玩家出拳:" + pValue); System.out.println(c.getName() + "电脑出拳:" + cValue); System.out.println("你输了" + ",电脑:" + c.getName() + "赢了"); c.setScore( c.getScore() + 1 ); } } // i++; // 提升完之后:调用显示结果的方法 showResult(); } //获取猜拳对应的中文 public String getFistValue(int fist){ String fistValue = ""; switch (fist) { case 1: fistValue = "石头"; break; case 2: fistValue = "剪刀"; break; case 3: fistValue = "布"; break; default: break; } return fistValue; } //最终结果 public void showResult() { System.out.println("最终结果:"); System.out.println(p.getName() +"最终积分: " + p.getScore()); System.out.println(c.getName() +"最终积分: " + c.getScore()); } }
package ppt10lang包.猜拳游戏; import java.util.Scanner; public class TestGame { public static void main(String[] args) { // 玩家对象 Person p = new Person("小白"); // 电脑对象 Computer c = new Computer("电脑CC"); Game game = new Game(p, c); // game.pk(); Scanner input = new Scanner(System.in); while (true) { game.pk(); System.out.println("是否继续,继续Y"); String str = input.next(); //忽略大小写 if (!"Y".equalsIgnoreCase(str)) { break; } } //未提升之前:调用显示结果 // game.showResult(); } }
标签:bst util bsp nbsp logs 比较 system player .sh
原文地址:http://www.cnblogs.com/0717GG/p/7084473.html