人机猜拳游戏求助~~~~

阳关下的大灰狼 2011-05-13 08:13:58
我写了一段小程序 测试出错:
程序测试结果为:

*******************
** 猜拳 **
*******************

请选择电脑玩家: 1.刘备 2.孙权 3.曹操
1
请输入您的姓名:111
111 VS 刘备
您要开始游戏么? y/n
y
请选择出拳: 1.剪刀 2.石头 3.布
1
您出拳:剪刀
刘备出拳:剪刀
请选择出拳: 1.剪刀 2.石头 3.布 //我想要的结果到这部应该是直接提示 您输了 或者 您赢了
1
您出拳:剪刀 //此步为多余
null出拳:剪刀 //此步为多余
打平~~~
您输了~~
是否继续? y/n


正常结果应该是:

*******************
** 猜拳 **
*******************

请选择电脑玩家: 1.刘备 2.孙权 3.曹操
1
请输入您的姓名:111
111 VS 刘备
您要开始游戏么? y/n
y
请选择出拳: 1.剪刀 2.石头 3.布
1
您出拳:剪刀
刘备出拳:剪刀
打平~~~
是否继续? y/n


以下是我写的有问题代码 请给位帮忙查看下

//定义用户类
import java.util.Scanner;
public class Person {
String name;
int num;
int fenShu;
Scanner input = new Scanner(System.in);
//用户输入姓名
public String showName(){
System.out.print("请输入您的姓名:");
name = input.next();
return name;
}
//玩家出拳
public int showPerson() {
System.out.println("请选择出拳: 1.剪刀 2.石头 3.布");
num = input.nextInt();
switch (num) {
case 1:
System.out.println("您出拳:剪刀");
break;
case 2:
System.out.println("您出拳:石头");
break;
case 3:
System.out.println("您出拳:布");
break;
}
return num;
}

}





//定义电脑类
import java.util.Scanner;
import java.util.Random;
public class Computer {
String name;
int num;

int fenShu;

Scanner input =new Scanner(System.in);
//选择电脑玩家
public String showName() {
System.out.println("请选择电脑玩家: 1.刘备 2.孙权 3.曹操");
int xuanZe = input.nextInt();
switch (xuanZe) {
case 1:
name = "刘备";
break;
case 2:
name ="孙权";
break;
case 3:
name = "曹操";
break;
}
return name;
}
//电脑自动出拳
public int showComputer() {
num = (int)((Math.random()*10)%3 +1);
switch(num) {
case 1:
System.out.println(name + "出拳:剪刀");
break;
case 2:
System.out.println(name + "出拳:石头");
break;
case 3:
System.out.println(name + "出拳:布");
break;
}
return num;
}
}








//定义游戏类
import java.util.Scanner;
public class Game {
int count; //对战次数
Person per; //Person用户类 对象名为 per
Computer com;
//显示开始界面
public void showChuShiHua() {
per = new Person();
com = new Computer();
count =0;
}
public void showStart() {
showChuShiHua();
System.out.println("\t\t*******************");
System.out.println("\t\t** 猜拳 开始 **");
System.out.println("\t\t*******************\n");
}

//每次输赢判断
public void showShuYing() {
showChuShiHua();
int perFist = per.showPerson();
int comFist = com.showComputer();
//比赛结果判断
if(per.num == 1 && comFist == 1 ||
per.num == 2 && com.num == 2 ||
per.num == 3 && com.num == 3){
System.out.println("打平~~~");
}
if(per.num == 1 && com.num == 3 ||
per.num == 2 && com.num == 1 ||
per.num == 3 && com.num == 2) {
System.out.println("您获胜~~");
}else{
System.out.println("您输了~~");
}
}
/* //得分计算
public void showFenShu() {
showChuShiHua();
int per1 = per.showPerson();
int com1 = com.showComputer();

if(per1 == 2 && com1 == 1 ||
per1 == 1 && com1 == 3 ||
per1 == 3 && com1 == 2){
per.fenShu++;
if(com.num > 0) {
com.fenShu--;
}
} else {
if(per.fenShu>0) {
per.fenShu--;
}
com.fenShu++;
} */

}





/*
*游戏开始测试类
*/
import java.util.Scanner;
public class GameStart {

public static void main(String[] args) {
Person person = new Person(); //实例化用户类对象为 person
Computer com = new Computer(); //实例化电脑类 对象为 com
Game game = new Game();

game.showStart(); //显示游戏开始界面
com.showName(); //选择电脑玩家
person.showName(); //输入玩家名
//玩家vs电脑
System.out.println(person.name + " VS " + com.name);
System.out.println("您要开始游戏么? y/n");
Scanner input = new Scanner(System.in);
String choice = input.next(); //开始游戏选择输入 输入y开始游戏
if(choice.equals("y")) {
boolean con;
do{
con = false;
person.showPerson(); //显示玩家出拳
com.showComputer(); //显示计算机出拳
game.showShuYing(); //显示每次结果
System.out.println("是否继续? y/n");
String queRen = input.next(); //继续选择y不继续选择n退出
if(queRen.equals("y")) {
con = true;
}
}while(con);

}


}
}
...全文
579 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
凉岑玉 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zfl1986 的回复:]
this.per 这个this是什么意思?
[/Quote]
当前对象··
茫茫大海 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zfl1986 的回复:]
this.per 这个this是什么意思?
[/Quote]
http://blog.csdn.net/fzfengzhi/archive/2008/03/12/2174406.aspx
楼主可以看看这个!
  • 打赏
  • 举报
回复
this.per 这个this是什么意思?
tianyu945473925 2011-05-13
  • 打赏
  • 举报
回复
package 论坛题目;

import java.util.Random;
import java.util.Scanner;

class Computer{
int num;
String name;
int p;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}

public void ChuQuan(int n){
if(n==1){
System.out.print("剪刀");
}
else if(n==2){
System.out.print("石头");
}
else if(n==3){
System.out.print("布");
}
}

public void diannao(){
Random rand=new Random();
p=rand.nextInt(3)+1;
// System.out.println("n="+n);
this.ChuQuan(p);
}
}

class You{
Computer compu=new Computer();
String youname=null,start=null;
// Scanner scan=new Scanner(System.in);
public void select(){
System.out.println("请选择电脑玩家:1、刘备 2、孙权 3、曹操");
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
if(t==1){
compu.setName("刘备");
compu.setNum(1);
}
else if(t==2){
compu.setName("孙权");
compu.setNum(2);
}
else if(t==3){
compu.setName("曹操");
compu.setNum(3);
}

System.out.println("请输入您的姓名……");
youname=new Scanner(System.in).nextLine();
System.out.println(youname+" VS "+compu.getName());

System.out.println("你要开始游戏吗?(y/n)");
start=new Scanner(System.in).next();

if(start.equals("y")){
System.out.println("请选择出拳:1、剪刀 2、石头 3、布");
int m=scan.nextInt();
if(m==1){
System.out.println("你出拳:剪刀");
System.out.print(compu.getName()+"出拳:");
compu.diannao();
System.out.println("");
if(compu.p==1){
System.out.println("打平");
}
else if(compu.p==2){
System.out.println("你输了");
}
else if(compu.p==3){
System.out.println("你赢了");
}

}
else if(m==2){
System.out.println("你出拳:石头");
System.out.print(compu.getName()+"出拳:");
compu.diannao();
System.out.println("");
if(compu.p==1){
System.out.println("你赢了");
}
else if(compu.p==2){
System.out.println("打平");
}
else if(compu.p==3){
System.out.println("你输了");
}
}
else if(m==3){
System.out.println("你出拳:布");
System.out.print(compu.getName()+"出拳:");
compu.diannao();
System.out.println("");
if(compu.p==1){
System.out.println("你输了");
}
else if(compu.p==2){
System.out.println("你赢了");
}
else if(compu.p==3){
System.out.println("打平");
}
}
}
else{
System.out.println("退出游戏");
}

}
}

public class LunTan_06 {
public static void main(String[] args) {
You you=new You();
you.select();
}
}
运行结果:
请选择电脑玩家:1、刘备 2、孙权 3、曹操
3
请输入您的姓名……
tianyu
tianyu VS 曹操
你要开始游戏吗?(y/n)
y
请选择出拳:1、剪刀 2、石头 3、布
2
你出拳:石头
曹操出拳:石头
打平
茫茫大海 2011-05-13
  • 打赏
  • 举报
回复

//定义用户类
import java.util.Scanner;

public class Person {
String name;
int num;
int fenShu;
Scanner input = new Scanner(System.in);

// 用户输入姓名
public String showName() {
System.out.print("请输入您的姓名:");
name = input.next();
return name;
}

// 玩家出拳
public int showPerson() {
System.out.println("请选择出拳: 1.剪刀 2.石头 3.布");
num = input.nextInt();
switch (num) {
case 1:
System.out.println("您出拳:剪刀");
break;
case 2:
System.out.println("您出拳:石头");
break;
case 3:
System.out.println("您出拳:布");
break;
}
return num;
}

}

//定义电脑类
import java.util.Scanner;
import java.util.Random;

public class Computer {
String name;
int num;

int fenShu;

Scanner input = new Scanner(System.in);

// 选择电脑玩家
public String showName() {
System.out.println("请选择电脑玩家: 1.刘备 2.孙权 3.曹操");
int xuanZe = input.nextInt();
switch (xuanZe) {
case 1:
name = "刘备";
break;
case 2:
name = "孙权";
break;
case 3:
name = "曹操";
break;
}
return name;
}

// 电脑自动出拳
public int showComputer() {
num = (int) ((Math.random() * 10) % 3 + 1);
switch (num) {
case 1:
System.out.println(name + "出拳:剪刀");
break;
case 2:
System.out.println(name + "出拳:石头");
break;
case 3:
System.out.println(name + "出拳:布");
break;
}
return num;
}
}

//定义游戏类
import java.util.Scanner;

public class Game {
int count; // 对战次数
Person per; // Person用户类 对象名为 per
Computer com;


/**
* 构造Game类时传入Person和Computer对象,方便在该类中打印每次的结果,并统计猜拳情况。
* ********************************************
* ********************************************
*/
public Game(Person per, Computer com) {
this.per = per;
this.com = com;
}


/**
* 此方法中的代码导致没打印结果,所以在此注释掉了
* ********************************************
* ********************************************
*/
// 显示开始界面
public void showChuShiHua() {
//per = new Person();
//com = new Computer();
//count = 0;
}

public void showStart() {
showChuShiHua();
System.out.println("\t\t*******************");
System.out.println("\t\t** 猜拳 开始 **");
System.out.println("\t\t*******************\n");
}

// 每次输赢判断
public void showShuYing() {
showChuShiHua();

/**
* 此处不用 per.showPerson()和com.showComputer(),
* 因为该方法只是猜拳后的打印结果和统计结果的代码。
* ********************************************
* ********************************************
*/
//int perFist = per.showPerson();
//int comFist = com.showComputer();
// 比赛结果判断
if (per.num == 1 && com.num == 1 || per.num == 2 && com.num == 2
|| per.num == 3 && com.num == 3) {
System.out.println("打平~~~");
}
if (per.num == 1 && com.num == 3 || per.num == 2 && com.num == 1
|| per.num == 3 && com.num == 2) {
System.out.println("您获胜~~");

/**
* 你赢了后你的分数加1,这里分数既是你赢的次数一
* ********************************************
* ********************************************
*/
per.fenShu ++;
} else {
System.out.println("您输了~~");

/**
* 你输了,电脑的分数加1
* ********************************************
* ********************************************
*/
com.fenShu ++;
}

/**
* 对战次数加1
* ********************************************
* ********************************************
*/
count++;//对战次数加1
}
/*
* //得分计算 public void showFenShu() { showChuShiHua(); int per1 =
* per.showPerson(); int com1 = com.showComputer();
*
* if(per1 == 2 && com1 == 1 || per1 == 1 && com1 == 3 || per1 == 3 && com1
* == 2){ per.fenShu++; if(com.num > 0) { com.fenShu--; } } else {
* if(per.fenShu>0) { per.fenShu--; } com.fenShu++; }
*/

}


/*
*游戏开始测试类
*/
import java.util.Scanner;

public class GameStart {

public static void main(String[] args) {
Person person = new Person(); // 实例化用户类对象为 person
Computer com = new Computer(); // 实例化电脑类 对象为 com

/**
* 改变Game的构造类,把person和com值传进去。方便在Game中打印每次的结果,并进行计数
* ********************************************
* ********************************************
*/
Game game = new Game(person,com);

game.showStart(); // 显示游戏开始界面
com.showName(); // 选择电脑玩家
person.showName(); // 输入玩家名
// 玩家vs电脑
System.out.println(person.name + " VS " + com.name);
System.out.println("您要开始游戏么? y/n");
Scanner input = new Scanner(System.in);
String choice = input.next(); // 开始游戏选择输入 输入y开始游戏
if (choice.equals("y")) {
boolean con;
do {
con = false;
person.showPerson(); // 显示玩家出拳
com.showComputer(); // 显示计算机出拳
game.showShuYing(); // 显示每次结果
System.out.println("是否继续? y/n");
String queRen = input.next(); // 继续选择y不继续选择n退出
if (queRen.equals("y")) {
con = true;
}
} while (con);
/**
* 输出猜拳结果
* ********************************************
* ********************************************
*/
System.out.println("总共猜了" + game.count + "次!");
System.out.println("你赢了" + person.fenShu + "次!");
System.out.println("电脑赢了" + com.fenShu + "次!");
System.out.println("战平了" + (game.count - person.fenShu - com.fenShu) + "次!");
}

}
}


我修改的地方加了注释,楼主可以看看!就改了Game和GameStart两个类。
tianyu945473925 2011-05-13
  • 打赏
  • 举报
回复
这个貌似还是比较容易实现的,我来编程试试看……

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧