剪刀石头布小游戏 java实现,代码如下
import java.util.Scanner;
public class 人机互博游戏{
public static void main(String[] args) {
System.out.println("------欢迎来到人机互搏猜拳小游戏-------");
System.out.println("请输入你的对战的英雄序号:1.狄仁杰 2.孙尚香 3.花木兰");
Scanner sc = new Scanner(System.in);
int enemy = sc.nextInt();
System.out.println("请录入你的英雄名字:");
String myself = sc.next();
String enemyName = "";
switch (enemy){
case 1:
enemyName = "狄仁杰";
break;
case 2:
enemyName = "孙尚香";
break;
case 3:
enemyName = "花木兰";
break;
}
System.out.println("敌人【" + enemyName + "】和【" + myself + "】即将开战!");
System.out.println("确认是否开始游戏?Y/N");
String isStart = sc.next();
if(isStart.equalsIgnoreCase("Y")){
int myScore = 0;
int enemyScore = 0;
for (int i = 1; i <= 5 ; i++) {
System.out.println("第" + i + "局游戏开始:");
System.out.println("请你出拳:1.石头 2.剪刀 3.布");
int choice = sc.nextInt();
int enemyChoice = (int)(Math.random()*3) + 1;
System.out.println("我的出拳结果是:" + change(choice));
System.out.println("敌人的出拳结果是:" + change(enemyChoice));
if((choice == 1 && enemyChoice == 2)||(choice == 2 && enemyChoice == 3)||(choice == 3 && enemyChoice == 1)){
System.out.println("我赢了!");
myScore++;
}else if((choice == 1 && enemyChoice == 1)||(choice == 2 && enemyChoice == 2)||(choice == 3 && enemyChoice == 3)){
System.out.println("平局了!");
}else{
System.out.println("你输了!");
enemyScore++;
}
}
if(myScore > enemyScore){
System.out.println("我最终取得胜利");
}else if(myScore == enemyScore){
System.out.println("人机平局");
}else{
System.out.println("系统最终取得胜利");
}
}else{
System.out.println("游戏结束,退出游戏");
}
}
public static String change(int choice){
String ch = "";
switch (choice){
case 1:
ch = "石头";
break;
case 2:
ch = "剪刀";
break;
case 3:
ch = "布";
break;
}
return ch;
}
}
|