您的位置:首页 > 编程语言 > Java开发

JAVA程序设计(07.2)-----面对对象设计练习 猜拳

2014-10-23 23:40 239 查看
1.妥妥的先建立对象

package com.lovo;

/**
* 类:猜拳游戏~ 暂定3胜制吧~
* @author Abe
*
*/
/**
* 构造器
*/

/**
* 属性: 1. 石头剪刀布 2. 循环次数 3. 各自胜利次数
*/
public class Morra {
private int finger = 0;
private int times = 0;
private int pcwin = 0;
private int youwin = 0;

/**
* 动作:电脑获取猜拳
*/
public void have() {
finger = (int) (Math.random() * 3 + 1);
}
/**
* 动作:猜拳转化为数字 1剪刀 2石头 3布
*/
public int change(String x) {
int yours = 3;
if (x.equals("剪刀")) {
yours = 1;
} else if (x.equals("石头")) {
yours = 2;
}
return yours;
}
/**
* 动作:比较输赢
*/
public String look(int yours) {
if (finger == yours) {
times++;
return "平局~~ 你们玩了" + times + "局了~";
} else if (finger == 1 && yours == 3 || finger == 2 && yours == 1
|| finger == 3 && yours == 2) {
pcwin++;
times++;
return "哎!这局电脑获胜了~~~你们玩了" + times + "局了~";
}
youwin++;
times++;
return "哦!这局你获胜了~~~你们玩了" + times + "局了~";
}
/**
* 调取胜利次数
*/
public int pcwin() {
return pcwin;
}
public int youwin() {
return youwin;
}
}


然后是主程序

package com.lovo;
/**
* 猜拳游戏开始~
* @author Abe
*/
import java.util.Scanner;

public class Morratest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Morra pc = new Morra();

do {
System.out.print("请输入你要猜拳的种类:1石头 2剪刀 3布");
int yours = sc.nextInt();
pc.have();
String hint = pc.look(yours);
System.out.println(hint);

} while (pc.pcwin() != 3 && pc.youwin() != 3);
if (pc.pcwin() == 3) {
System.out.println("电脑累计胜利3次~获得了最终胜利。");
} else {
System.out.println("你累计胜利3次~获得了最终胜利!!");
}
sc.close();
}
}


老师的方法~更好的封装~ 有必要还可以加 输赢次数什么的 懒得做了 判断也没做 据说以后会讲 妥妥的~

先是类

package com.lovo;

/**
* 类:猜拳游戏~
* @author Abe
*
*/

/**
* 构造器
*/
public class Morra {
public Morra(String str) {
this.name = str;
}

/**
* 属性: 1. 石头剪刀布
*/
private String name = "";
private int finger = 0;

/**
* 动作:电脑获取猜拳
*/
public void setFinger() {
this.finger = (int) (Math.random() * 3 + 1);
}

/**
* 动作:获取玩家输入的猜拳(1~3)
*/
public void setFinger(int x) {
this.finger = x;
}

/**
* 动作:猜拳转化为数字 1剪刀 2石头 3布
*/
public String change(int x) {
if (x == 1) {
return "1剪刀";
} else if (x == 2) {
return "2石头";
} else {
return "3布 ";
}
}

/**
* 调取名字,出的什么拳
*/
public String getName() {
return name;
}
public int getFinger() {
return finger;
}

/**
* 判断输赢 并返回字符串
* @param other
* @return
*/
public String open(Morra other) {
switch (this.getFinger()) {
case 1:
switch (other.getFinger()) {
case 1:
return "平局";
case 2:
return other.getName() + "~ 赢了~~";
default:
return this.getName() + "~ 赢了~~";
}
case 2:
switch (other.getFinger()) {
case 1:
return this.getName() + "~ 赢了~~";
case 2:
return "平局";
default:
return other.getName() + "~ 赢了~~";
}
default:
switch (other.getFinger()) {
case 1:
return other.getName() + "~ 赢了~~";
case 2:
return this.getName() + "~ 赢了~~";
default:
return "平局";
}
}
}
}

程序依旧那么简单~
package com.lovo;

/**
* 猜拳游戏开始~
* @author Abe
*/
import java.util.Scanner;

public class Morratest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Morra human = new Morra("小和尚");
Morra pc = new Morra("法轮");

System.out.print("请选择你要出的拳:1剪刀 2石头 3布");
human.setFinger(sc.nextInt());
System.out.println(human.getName() + "出了"
+ human.change(human.getFinger()));
pc.setFinger();
System.out.println(pc.getName() + "出了" + pc.change(pc.getFinger()));
System.out.println(human.open(pc));

sc.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐