文档库 最新最全的文档下载
当前位置:文档库 › Java人机猜拳源代码

Java人机猜拳源代码

/**
*
* @author student
*
*/
public class Computer {//定义电脑类
String name="电脑";
int score=0;
public int showFist(){//电脑出拳
int show=(int)(Math.random()*10)%3+1;//随即出现的1~3之间的整数
switch(show){
case 1:
System.out.println("电脑出拳:剪刀");
break;
case 2:
System.out.println("电脑出拳:石头");
break;
case 3:
System.out.println("电脑出拳:布");
break;
}
return show;//返回show
}

}




































e84a5e507b91123c92fc470d
import java.util.*;
/**
*
* @author student
*
*/
public class Person {//创建人类
String name="匿名";
int score=0;
public int showFist(){//人出拳
System.out.println("请猜拳(1-剪刀,2-石头,3-布(请选择:))");
Scanner in=new Scanner(System.in);
int show=in.nextInt();//请输入数字
switch(show){
case 1:
System.out.println("您出拳:剪刀");
break;
case 2:
System.out.println("您出拳:石头");
break;
case 3:
System.out.println("您出拳:布");
break;
}
return show;//返回后展示

}
}

import java.util.*;
/**
*
* @author
*
*/
public class Game {//创建游戏类
Person person;//人类 ,人
Computer computer;//电脑类,电脑
int count;//局数

public void initial(){//游戏初始化界面方法
person=new Person();//人物初始化
computer=new Computer();//电脑任务初始化
count=0;
}
public void startGame(){//开始游戏方法
System.out.println("*欢迎进入人机猜拳*");
System.out.println("***************");
System.out.println("****猜拳开始****");
System.out.println("***************");
System.out.println("出拳规则:1.剪刀,2.石头,3.布");
Scanner in=new Scanner(System.in);
System.out.print("请选择对方角色:(1.刘备,2.孙权,3.曹操)");
int role=in.nextInt();
System.out.print("请输入您的名字");
https://www.wendangku.net/doc/a12089897.html,=in.next();//person
switch(role){
case 1:
https://www.wendangku.net/doc/a12089897.html,="刘备";
break;
case 2:
https://www.wendangku.net/doc/a12089897.html,="孙权";
break;
case 3:
https://www.wendangku.net/doc/a12089897.html,="曹操";
break;
}
System.out.println(https://www.wendangku.net/doc/a12089897.html,+"VS"+https://www.wendangku.net/doc/a12089897.html,+"\n");
System.out.println("是否开始游戏(y/n)?");
String con=in.next();//while循环String变量con初始值
//while循环结构
while(con.equals("y")){
int perShow=person.showFist();
int comShow=computer.showFist();
if(perShow==comShow){
System.out.println("真衰!平局!");
count++;
}else if(perShow==1&&comShow==2||perShow==2&&comShow==3||perShow==3&&comShow==1){
System.out.println("真悲哀!您输了!");
computer.score++;
count++;
}else if(perShow==2&&comShow==1||perShow==3&&comShow==2||perShow==1&&comShow==3){
System.out.println("恭喜!你赢了!");
person.score++;
count++;
}
System.out.pr

intln("是否开始下一轮?y/n");
con = in.next();
}
System.out.println(https://www.wendangku.net/doc/a12089897.html, + "\tVS\t" + https://www.wendangku.net/doc/a12089897.html,);
System.out.println("对战次数:" + count);
System.out.println(https://www.wendangku.net/doc/a12089897.html, + "\tVS\t" + https://www.wendangku.net/doc/a12089897.html,);
System.out.println(computer.score + "\tVS\t" + person.score);
if (computer.score > person.score) {
System.out.println("结果:*-*,你输了啊,嘿嘿!!");
} else if (computer.score == person.score) {
System.out.println("结果:*……*,唉!平局啊,好,下次再一决高下!!拜拜");
} else {
System.out.println("结果:居然让你小子赢了!!再来再来!!");
}

}
}


public class Test {//游戏测试类main\方法

/**
* @param args
*/
public static void main(String[] args) {
Game game=new Game();//创建对象game
game.initial();//游戏初始界面方法
game.startGame();//、调用开始游戏方法界面

}

}

相关文档