import java.util.Scanner; public class Game { 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; //按照流程写程序 //欢迎界面 System.out.println("**********************************"); System.out.println(" 游戏:剪刀石头布"); System.out.println(" 作者:XXXXXX"); System.out.println(" 版本:v1.0"); System.out.println("*********************************"); System.out.println("欢迎进入本游戏,请输出游戏玩家的名字:"); //获取输入设备 Scanner scanner = new Scanner(System.in); //结合上下文的变量来决定当前等式的变量,nextLine()游戏玩家的名字可以带空格 player_name=scanner.nextLine(); //选择电脑的玩家 System.out.println("********************************"); System.out.println("1-----蜡笔小新"); System.out.println("2-----葫芦娃"); System.out.println("*********************************"); //需要用户选择玩家 String com_no=scanner.next(); if (com_no.equals("1")){ com_name="蜡笔小新"; }else if(com_no.equals("2")){ com_name="葫芦娃"; }else{ com_name="匿名用户"; } //玩家出拳 //玩家出拳 System.out.println("@@@@@@@@@@@@@@@@@@@@"); System.out.println("1------剪刀"); System.out.println("2------石头"); System.out.println("3------布"); System.out.println("@@@@@@@@@@@@@@@@@@@@"); System.out.println("请输入出拳的代表数字:"); //玩家输入的是int型,所以接受用nextInt型 player_choice=scanner.nextInt(); //电脑出拳随机用math.random(),取整用math.round() //(int)强制转换 com_choice=(int)Math.round(Math.random()*2+1);//将long型转换成int型 //判断数值计算用== if (player_choice-com_choice==1||player_choice-com_choice==-2){ System.out.println("玩家"+player_name+"赢"); } else if(com_choice-player_choice==1||com_choice-player_choice==-2){ System.out.println("电脑"+com_name+"赢"); } else{ System.out.println("平局"); } } }
游戏(石头剪刀布)
最新推荐文章于 2024-10-16 10:13:45 发布