java人机猜拳-石头剪刀布

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java人机猜拳

1.首先定义一个用户类:代码如下

package mypackage;

import java.util.*;

public class Person {

String name="";

int score;

public int showFist()

{

System.out.println("请出拳:1.剪刀2.石头3.布(输入相应数字)");

Scanner input =new Scanner(System.in);

int number=input.nextInt();

switch(number)

{

case 1:

System.out.println("玩家出:剪刀");

return number;

case 2:

System.out.println("玩家出:石头");

return number;

case 3:

System.out.println("玩家出:布");

return number;

default:

System.out.println("你出拳:剪刀");

return number;

}

}

}

2.定义一个计算机类

package mypackage;

public class Computer {

int max =3;

int min =1;

int number= (int)(Math.random()*(max-min))+min;

int score;

String name="电脑";

public int showcomputer()

{

switch(number)

{

case 1:

System.out.println("电脑出:剪刀");

return number;

case 2:

System.out.println("电脑出;石头");

return number;

case 3:

System.out.println("电脑出:布");

return number;

default:

System.out.println("电脑出:剪刀");

return number;

}

}

}

3.创建一个游戏类

package mypackage;

import java.util.*;

public class StartGame

{

public int Initial()

{

System.out.println("----------欢迎进入游戏世界----------");

System.out.println("");

System.out.println("\t****************");

System.out.println("\t** 猜拳,开始**\t\t");

System.out.println("\t****************");

System.out.println("");

System.out.println("出拳规则:1.剪刀2.石头3.布");

System.out.println("请选择对方角色(1.刘备2.孙权3.曹操):");

Scanner input =new Scanner(System.in);

int number=input.nextInt();

switch(number)

{

case 1:

System.out.print("刘备");

return number;

case 2:

System.out.print("孙权");

return number;

case 3:

System.out.print("曹操");

return number;

default:

System.out.print("你选择了刘备作战");

return number;

}

}

public static void main(String[] args)

{

//完善游戏类的startGame()方法,实现一局对战

Computer computer =new Computer();

Person player =new Person();

StartGame come =new StartGame();

Scanner input =new Scanner(System.in);

come.Initial();

System.out.println("");

System.out.println("要开始么?y/n\n\n");

String con =input.next();

int count=0;

while(con.equalsIgnoreCase("y"))

{

int perFist=player.showFist();

int compFist=computer.showcomputer();

System.out.println("双方对战次数:" + count);

if((perFist==1&&compFist==1)||(perFist==2&&compFist==2)||(perFist==3&&com pFist==3))

{

System.out.println("结果:平局,真衰!");

count++;

}else

if((perFist==1&&compFist==3)||(perFist==2&&compFist==1)||(perFist==3&&compFist ==2))

{

System.out.println("结果:恭喜,你赢了!");

player.score++;

}

else{

System.out.println("结果说,你输了,真笨!\n");

count++;

computer.score++;

}

System.out.println(+ "积分为:" + player.score+ "\t\t" + + "积分为:" + computer.score);

System.out.println("是否继续?y/n");

con =input.next();

}

while(con.equals("n"))

相关文档
最新文档