package com.njwbhz.path0214;
import java.util.Scanner;
public class Mora {
public static void main(String[] args) {
//敌方的游戏初始化参数
Scanner enemyInput = new Scanner(System.in);
int enemy;
String enemyName = "";
int enemyFlag = 0;
//玩家自己的游戏参数
Scanner playerInput = new Scanner(System.in);
String playerName = "";
//是否开始的参数设置
Scanner isStartInput = new Scanner(System.in);
String isStart = "";
//出拳参数
Scanner playerPunchInput = new Scanner(System.in);
int playerPunch = 0;
int enemyPunch = 0;
String playerPunchName = "";
String enemyPunchName = "";
int playerPunchFlag = 0;
//统计
//对站次数
int matchNumber = 0;
//敌方玩家得分
float enemyScore = 0;
//玩家得分
float playerScore = 0;
//欢迎部分
System.out.println("——————————欢迎老板——————————");
System.out.println("\n");
System.out.println("\t话不多说,开始猜拳");
System.out.println("————————————————————————");
//初始化部分
//介绍游戏操作
System.out.println("赢一把加一分,其余情况均不加分");
System.out.println("出拳规则:1. 剪刀 2. 石头 3. 布");
//选择对方的游戏角色
System.out.print("请输入对方的角色(1:刘备 2:孙权 3:曹操):");
//保存敌方角色的初始化数据
while(enemyFlag == 0){
enemy = enemyInput.nextInt();
switch (enemy){
case 1:
enemyName = "刘备";
enemyFlag = 1;
break;
case 2:
enemyName = "孙权";
enemyFlag = 1;
break;
case 3:
enemyName = "曹操";
enemyFlag = 1;
break;
default:
System.out.println("老板,你输入的有问题,请重新输入");
enemyFlag = 2;
break;
}
if (enemyFlag == 1){
//如果输入正确,程序继续运行
break;
}else{
enemyFlag = 0;
//如果输入错误,重新进行游戏的初始化
continue;
}
}
//恢复初始化
enemyFlag = 0;
//代码测试
// System.out.println("你选择的敌方游戏角色的名字为:" + enemyName );
//玩家自己的游戏ID设置
System.out.print("请输入你的姓名:");
playerName = playerInput.next();
//代码测试
// System.out.print(playerName);
System.out.println("【" + playerName + "】" + "对战" + "【" + enemyName + "】");
System.out.println("\n");
//游戏开始部分
do {
//提示玩家出拳
System.out.print("请出拳:1. 剪刀 2. 石头 3. 布(输入相应的数字):");
//保存玩家出拳数据
while(playerPunchFlag == 0){
playerPunch = playerPunchInput.nextInt();
switch (playerPunch){
case 1:
playerPunchName = "剪刀";
playerPunchFlag = 1;
break;
case 2:
playerPunchName = "石头";
playerPunchFlag = 1;
break;
case 3:
playerPunchName = "布";
playerPunchFlag = 1;
break;
default:
System.out.println("老板,你输入的有问题,请重新输入");
playerPunchFlag = 2;
break;
}
if (playerPunchFlag == 1){
//如果输入正确,程序继续运行
break;
}else{
playerPunchFlag = 0;
//如果输入错误,重新进行游戏的初始化
continue;
}
}
//恢复初始化
playerPunchFlag = 0;
//输出玩家出的是什么拳
System.out.println(playerName + "出拳:" + playerPunchName);
//保存敌方出拳数据
enemyPunch = (int)(Math.random() * 3 + 1);
switch (enemyPunch){
case 1:
enemyPunchName = "剪刀";
break;
case 2:
enemyPunchName = "石头";
break;
case 3:
enemyPunchName = "布";
break;
}
//输出敌方玩家出的是什么拳
System.out.println(enemyName + "出拳:" + enemyPunchName);
//游戏结果公布
if (playerPunch == enemyPunch){
System.out.println("平局");
}else if(playerPunch == 1 && enemyPunch == 2){
System.out.println("请" + playerName + "再接再厉");
enemyScore++;
}else if(playerPunch == 1 && enemyPunch == 3){
System.out.println("恭喜" + playerName + "获得胜利");
playerScore++;
}else if(playerPunch == 2 && enemyPunch == 1){
System.out.println("恭喜" + playerName + "获得胜利");
playerScore++;
}else if(playerPunch == 2 && enemyPunch == 3){
System.out.println("请" + playerName + "再接再厉");
enemyScore++;
}else if(playerPunch == 3 && enemyPunch == 1){
System.out.println("请" + playerName + "再接再厉");
enemyScore++;
}else if(playerPunch == 3 && enemyPunch == 2){
System.out.println("恭喜" + playerName + "获得胜利");
playerScore++;
}
matchNumber++;
//询问玩家是否进入下一局游戏
System.out.println("\n");
System.out.println("是否开始下一轮(y/n)");
isStart = isStartInput.next();
}while(isStart.equalsIgnoreCase("y"));
//游戏统计
System.out.println("游戏统计");
System.out.println("总共对战:" + matchNumber + "局");
System.out.println("玩家\t\t得分");
System.out.println(playerName + "\t\t" + playerScore);
System.out.println(enemyName + "\t\t" + enemyScore);
System.out.println(playerName + "的胜率为" + playerScore / enemyScore * 100 + "%");
}
}
01-19