JAVA程序设计(07.2)-----面对对象设计练习 猜拳
2014-10-23 23:40
239 查看
1.妥妥的先建立对象
然后是主程序
老师的方法~更好的封装~ 有必要还可以加 输赢次数什么的 懒得做了 判断也没做 据说以后会讲 妥妥的~
先是类
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();
}
}
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();
}
}
相关文章推荐
- JAVA程序设计(09)-----面对对象设计初级应用 龟兔赛跑
- JAVA程序设计(07.3)-----面对对象设计 时钟
- JAVA程序设计(09)-----面对对象设计初级应用 奥特曼打小怪兽
- Java学习----day6——(2)面对对象程序设计总结【题目详解】
- 面向对象程序设计上机练习一(函数重载)(java)
- Java学习----day5——(1)面对对象程序设计基础篇
- sdut_java_面向对象程序设计上机练习一(函数重载)
- JAVA程序设计(07.1)-----用面对对象设计的方法编制程序练习 1~100猜数
- Java学习----day5——(3)面对对象程序设计基础篇(具体篇)
- Java sdut acm 1140 面向对象程序设计上机练习一(函数重载)
- JAVA进阶 面向对象程序设计——第5周 设计原则(耦合和聚合,代码结构设计,程序维护拓展发展)
- java 语言程序设计-李尊朝 第6章 类和对象 练习答案
- 20162319 实验二 Java面对对象程序设计 实验报告
- Java三个月 - 面向对象程序设计OOP的乐趣 - 程序人生
- Java面向对象程序设计之接口应用
- 越深入java 就觉得java做得越烂,java设计问题 之 : 对象占用内存空间设计
- 第1章 Java基本概念及环境配置——FAQ1.02 面向对象程序设计的基本特征有哪些?
- 七、java的面向对象程序设计(详解equals方法)
- 有关JAVA的面向对象程序设计方法较全面介绍的一本书
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习5