本文主要是介绍华恩JAVA班第36天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
华恩JAVA班第36天
猜拳游戏:
package com.zjj;
import java.util.Scanner;
*文件名称:Person.java
*功能说明:
*开发人员:Ziu Ginzeon
*创建时间:2013年8月2日 下午3:18:48
public class Person {
private String name;
private int score;
public Person(String name, int score) {
this.name = name;
this.score = score;
}
public int showFist(){
Scanner in = new Scanner(System.in);
int a= in.nextInt();
if(a == 1){
System.out.println("你出拳:剪刀");
}else if(a == 2){
System.out.println("你出拳:石头");
}else if(a == 3){
System.out.println("你出拳:布");
}else{
System.out.println("输入错误!");
}
return a;
}
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;
}
}
package com.zjj;
*文件名称:Computer.java
*功能说明:
*开发人员:Ziu Ginzeon
*创建时间:2013年8月2日 下午3:39:54
public class Computer {
private String name;
private int score;
public Computer(String name, int score) {
super();
this.name = name;
this.score = score;
}
public int cptShowFist(){
int b = (int)(Math.random()*3);
if(b == 1){
System.out.println(getName()+"出拳:剪刀");
}else if(b == 2){
System.out.println(getName()+"出拳:石头");
}else if(b == 3){
System.out.println(getName()+"出拳:布");
}
return b;
}
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;
}
}
package com.zjj;
import java.util.Scanner;
*文件名称:Game.java
*功能说明:
*开发人员:Ziu Ginzeon
*创建时间:2013年8月2日 下午3:19:19
public class Game {
public static void main(String[] args){
int n = 0;
int pScore = 0;
int cScore = 0;
Scanner in = new Scanner(System.in);
String comName = null;
System.out.println("*************************");
System.out.println("**********猜拳,开始*********");
System.out.println("*************************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.println("请选择对方的角色:1.刘备2.孙权3.曹操");
int t = in.nextInt();
if(t == 1){
comName = "刘备";
}else if(t == 2){
comName = "孙权";
}else if(t == 3){
comName = "曹操";
}else{
System.out.println("输入错误");
}
Computer computer = new Computer(comName,0);
System.out.println("请输入你的姓名:");
String name = in.next();
System.out.println(name+"VS"+comName+" 对战");
while(true){
Person person = new Person(name,0);
System.out.println("请出拳:1.剪刀 2.石头 3.布");
int w = person.showFist() - computer.cptShowFist();
if(w == 0){
System.out.println("平局");
}else if(w == 1||w == -2){
System.out.println("赢");
person.setScore(pScore++);
}else{
System.out.println("输");
computer.setScore(cScore++);
}
System.out.println("是否继续(y/n)");
String c = in.next();
if(c.equals("n")){
n++;
break;
}else{
n++;
}
}
System.out.println("对决次数:"+n);
System.out.println("姓名"+"\t得分"+"\n"+name+"\t"+pScore+"\n"+comName+"\t"+cScore);
System.out.println("呵呵,继续努力");
}
}
更多信息可以参见同学富晓磊的博客:http://blog.sina.com.cn/u/1798827371
这篇关于华恩JAVA班第36天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!