版权声明:版权所有@万星明 https://blog.csdn.net/qq_19533277/article/details/83242849
通过构建玩家类,计算机类,游戏类,三个类,实现了简单的人机猜拳小游戏。
/**
* @author 万星明
* @version 创建时间:2018年10月20日 下午3:01:09
* 计算机类
*/
public class Computer {
public static String name = "计算机";
public static int score = 0;
//出拳方法
public static int finger() {
int data = (int)(Math.random()*3+1);
switch(data) {
case 1:
System.out.println(name+"出拳:剪刀");
break;
case 2:
System.out.println(name+"出拳:石头");
break;
case 3:
System.out.println(name+"出拳:布");
break;
}
return data;
}
}
import java.util.Scanner;
/**
* @author 万星明
* @version 创建时间:2018年10月20日 下午2:46:49
* 用户类
*/
public class User {
static Scanner sc = new Scanner(System.in);
public static String name = "万星明";
public static int score=0;
//出拳方法
public static int finger() {
System.out.println("请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
int data = sc.nextInt();
switch(data) {
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
default:
System.out.println("输入错误!");
break;
}
return data;
}
}
import java.util.Scanner;
/**
* @author 万星明
* @version 创建时间:2018年10月20日 下午3:06:39
* 游戏类
*/
public class Game {
static Scanner sc = new Scanner(System.in);
User user;
Computer computer;
static int fightFrequency=0;
//初始化方法
public static void initial() {
System.out.println("----------------欢迎进入游戏世界----------------");
System.out.println("\n\t\t\t\t************");
System.out.print("\t\t\t\t**猜拳**开始**");
System.out.print("\n\t\t\t\t************");
System.out.println("\n出拳规则:1.剪刀 2.石头 3.布");
}
//开始游戏方法
public static void startGame() {
System.out.printf("请选择计算机角色:1.刘备 2.曹操 3.孙权:");
int answer = sc.nextInt();
System.out.println("请输入你的姓名:");
User.name = sc.next();
switch(answer) {
case 1:
Computer.name = "刘备";
System.out.println(User.name+" VS "+Computer.name);
break;
case 2:
Computer.name = "曹操";
System.out.println(User.name+" VS "+Computer.name);
break;
case 3:
Computer.name = "孙权";
System.out.println(User.name+" VS "+Computer.name);
break;
default:
System.out.println("无此人物选项!");
break;
}
}
//第一次游戏方法
public static void showFirst() {
initial();
startGame();
int userNum = User.finger();
int computerNum = Computer.finger();
calResult(userNum, computerNum);
while(true) {
System.out.println("是否开始下一轮?(y/n):");
String answer = sc.next();
if(answer.equalsIgnoreCase("y")) {
int userNum1 = User.finger();
int computerNum1 = Computer.finger();
calResult(userNum1, computerNum1);
}else {
break;
}
}
}
//计算结果方法
public static void calResult(int userNum,int computerNum) {
if((userNum==1&&computerNum==3)||(userNum==2&&computerNum==1)||(userNum==3&&computerNum==2)) {
User.score++;
System.out.println("结果说:^_^你赢了,帅哥!");
}else if((userNum==1&&computerNum==2)||(userNum==2&&computerNum==3)||(userNum==3&&computerNum==1)){
Computer.score++;
System.out.println("结果说:^_^你输了,笨蛋!");
}else {
System.out.println("结果说:^_^竟然是平局!!");
}
fightFrequency++;
}
//显示最终游戏结果方法
public static void showResult() {
System.out.println(Computer.name+":"+Computer.score+" VS "+User.score+":"+User.name);
System.out.println("对战次数:"+fightFrequency);
if(Computer.score>User.score) {
System.out.println("笨笨,加油啊!虽然你输了,但是!你萌啊!");
}else if(Computer.score<User.score) {
System.out.println("小伙子,厉害哦!电脑都赢不了你了!");
}else {
System.out.println("你也就和电脑一般智商,一般般!");
}
}
//主运行方法
public static void main(String[] args) {
showFirst();
showResult();
}
}