任务
完成人机猜拳互动游戏的开发
主要功能
选取对战角色
猜拳
记录分数
测试代码
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game aGame = new Game();
aGame.initial();
System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
Scanner input=new Scanner(System.in);
int xuanze1 = input.nextInt();
aGame.juese = xuanze1;
aGame.startGame1();
System.out.print("要开始吗?(y/n)");
String yn = input.next();
if (yn.equals("y")) {
aGame.starString2();
aGame.startString3();
}else {
System.out.println("输入有误!");
}
}
}
Game类
public class Game {
int juese=0;
int caiquan=0;
int count1=0;
int count2=0;
int count3=0;
String jiqi;
String xuanze="y";
//初始化游戏
public String initial() {
System.out.println("----------------欢迎进入游戏世界----------------");
System.out.println(" ");
System.out.println(" ************** ");
System.out.println(" ** 猜拳,开始 **");
System.out.println(" ************** ");
System.out.println(" ");
System.out.println("出拳规则:1、剪刀 2、石头 3、布");
return " ";
}
//选择电脑名称
public String startGame1(){
if(juese==1){
jiqi="刘备";
System.out.println("您选择了刘备对战");
}else if(juese==2){
jiqi="孙权";
System.out.println("您选择了孙权对战");
}else if(juese==3){
jiqi="曹操";
System.out.println("您选择了曹操对战");
}else {
System.out.println("输入出错!");
}
System.out.println(" ");
return " ";
}
public void starString2(){
Scanner input=new Scanner(System.in);
while (xuanze.equals("y")){
System.out.print("请出拳1、剪刀 2、石头 3、布(输入相应数字):");
int xuanze2 = input.nextInt();
if (xuanze2==1) {
System.out.println("你出:剪刀");
}else if (xuanze2==2) {
System.out.println("你出:石头");
}else if (xuanze2==3) {
System.out.println("你出:布");
}else {
System.out.println("输入出错!");
}
//调用Computer类,电脑随机出拳
Computer bComputer = new Computer();
int b =bComputer.Random();
if (b==1) {
System.out.println(jiqi + "出:剪刀");
}else if (b==2) {
System.out.println(jiqi + "出:石头");
}else {
System.out.println(jiqi + "出:布");
}
//判断输赢,输出结果
if(xuanze2==1 && b==1){
System.out.println("结果:你们俩平手了!");
}else if (xuanze2==1 && b==2) {
System.out.println("结果:很遗憾,你输了!");
count3++;
}else if (xuanze2==1 && b==3) {
System.out.println("结果:恭喜你,你赢了!");
count2++;
}else if (xuanze2==2 && b==1) {
System.out.println("结果:恭喜你,你赢了!");
count2++;
}else if (xuanze2==2 && b==2) {
System.out.println("结果:你们俩平手了!");
}else if (xuanze2==2 && b==3) {
System.out.println("结果:很遗憾,你输了!");
count3++;
}else if (xuanze2==3 && b==1) {
System.out.println("结果:很遗憾,你输了!");
count3++;
}else if (xuanze2==3 && b==2) {
System.out.println("结果:恭喜你,你赢了!");
count2++;
}else {
System.out.println("结果:你们俩平手了!");
}
System.out.println(" ");
System.out.print("是否开启下一轮(y/n):");
xuanze = input.next();
count1++;
}
}
public String startString3() {
if (xuanze.equals("n")) {
System.out.println("-------------------------------------------");
System.out.println(jiqi + " VS 玩家");
System.out.println("对战次数:" + count1);
System.out.println(" ");
System.out.println("姓名 得分");
System.out.println("玩家 " + count2);
System.out.println(jiqi+" "+count3);
System.out.println(" ");
if (count2>count3) {
System.out.println("结果:恭喜恭喜!");
}else if (count2==count3) {
System.out.println("你们平局了哟!");
}else {
System.out.println("很遗憾,你输了,下次加油!");
}
}
return " ";
}
}
Computer类
public class Computer {
int r;
public int Random(){
r = new Random().nextInt(3)+1;
return r;
}
}
Java新手,如有大佬们不认可的,请多多指教。