石头剪刀布小游戏生成
import java.util.Scanner;
public class WeGameone {
public static void main(String[] args) {
//定义全局变量,玩家名字player_name,电脑名字com_name,玩家出拳player_choice,电脑出拳com_choice
String player_name;
String com_name;
int player_choice;
int com_choice;
//增加的bug变量,玩家的出拳名称 player_choice_name 电脑出拳名称 com_choice_name
String player_choice_name;
String com_choice_name;
//游戏界面
System.out.println("*********************************");
System.out.println(" 游戏:剪刀石头布");
System.out.println(" 作者:XXXXXX");
System.out.println(" 版本:v9.9");
System.out.println("*********************************");
System.out.println("欢迎进入本游戏,请输出游戏玩家的名字:");
//获取输入设备
Scanner scanner = new Scanner(System.in);
player_name=scanner.nextLine();
if(player_name.trim().equals("")){
player_name="匿名玩家";
}
//选择电脑的玩家
System.out.println("********************************");
System.out.println("1-----电脑1");
System.out.println("2-----电脑2");
System.out.println("*********************************");
//需要用户选择玩家
String com_no=scanner.next();
switch(com_no){
case "1":
com_name="电脑1";
break;
case "2":
com_name="电脑2";
break;
default:
com_name="匿名用户";
}
//玩家出拳
System.out.println("****请玩家选择出拳的代表数字*********");
System.out.println("1------剪刀");
System.out.println("2------石头");
System.out.println("3------布");
System.out.println("*********************************");
//玩家出拳为了运算方便,使用数字,next()接收字符串
String tmp_choice=scanner.next();
switch(tmp_choice){
case "1":
player_choice=Integer.parseInt(tmp_choice);
break;
case "2":
player_choice=Integer.parseInt(tmp_choice);
break;
case "3":
player_choice=Integer.parseInt(tmp_choice);
break;
default:
player_choice=(int)Math.round(Math.random()*2+1);
}
switch(player_choice){
case 1:
player_choice_name="剪刀";
break;
case 2:
player_choice_name="石头";
break;
default:
player_choice_name="布";
}
com_choice=(int)Math.round(Math.random()*2+1);
switch(com_choice){
case 1:
com_choice_name="剪刀";
break;
case 2:
com_choice_name="石头";
break;
default:
com_choice_name="布";
}
if(player_choice-com_choice==1 || player_choice-com_choice==-2){
System.out.println("玩家:"+player_name+",出拳:"+player_choice_name+"赢");
System.out.println("电脑:"+com_name+",出拳:"+com_choice_name+"输");
}else if(com_choice-player_choice==1 || com_choice-player_choice==-2){
System.out.println("电脑:"+com_name+",出拳:"+com_choice_name+"赢");
System.out.println("玩家:"+player_name+",出拳:"+player_choice_name+"输");
}else{
System.out.println("电脑:"+com_name+",出拳:"+com_choice_name+"平局");
System.out.println("玩家:"+player_name+",出拳:"+player_choice_name+"平局");
}
}
}
代码仅供参考