学习路上的小游戏

public class Demon {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String  answer = "y";
        String  name = null;
        do {
            System.out.println("欢迎进入我的武侠世界,请大侠起一个响当当的名号闯荡江湖吧:");
            name = scanner.nextLine();
            System.out.println("头衔:【初入江湖】");
            System.out.println("江湖名号" + name);
            System.out.println(name + "大侠满意您的江湖名号吗(y/n)");
            answer = scanner.nextLine();
        }while (!"y".equals(answer));
        System.out.printf("【%s】大侠重入江湖,宵小之辈苏苏退避!\n",name);
        scanner.close();
    }

}

猜数字游戏
public class Demo05 {
    public static void main(String[] args) {
        //用户猜的数字
        //用户猜的次数
        final int price = ((int)(Math.random() *1000) % 9 + 1)*1000;//用户猜的数字
        int guessPrine = -1;
        int count = 0; //用户猜总的次数
        String prizeName = null;
        for (int i = 0; i < 5 ; i++) {
            System.out.println("请输入神秘商品的单价:(" + ++count + "次)");
            guessPrine = new Scanner(System.in).nextInt();
            if(guessPrine == price){
                if(count == 1){
                    System.out.println("厉害了,一次就猜中了");
                    prizeName = "iPhone8S Plus Plus 土豪金";
                }else if (count >=2 && count<=3){
                    prizeName = "华为荣耀手环Zero";
                }else{
                    prizeName = "小米蓝牙耳机青春版";
                }
                break;
            }else if (guessPrine < price){
                System.out.println("小了,请继续!");
            }else if (guessPrine > price){
                System.out.println("大了,请继续!");
            }
        }
        if(null == prizeName){
            System.out.println("很遗憾,五次机会已用完,期待下一次吧");
        }else{
            System.out.println("恭喜您中奖了,奖品为:" + prizeName);
        }
    }
}

 PK小游戏

public class Demo03 {
    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        Scanner input = new Scanner(System.in);
        //播放音乐代码
        File sound1=new File("D:\\拳皇.wav");//java只支持wav格式
        AudioClip sound_choose= Applet.newAudioClip(sound1.toURL());
        sound_choose.play();//播放

        //随机数字:
        //Math.random()     //[0.0,1.0)

        //随机数公式:a - b;
        //(int)(Math.random()*10000)%(b - a + 1) + a;
        //(int)(Math.random()*10000)%11 + 5 [5~15)
        String name;
        System.out.print("请输入玩家名称:");
        String userName = input.next();
        String comName = "大蛇";
        System.out.println(userName + "VS" + comName);
        int hp1 = 100;
        int hp2 = 100;//双方的HP
        int attack1 = 0,attack2= 0;//双方的攻击力
        while (hp2 >= 0 && hp2 >= 0){
            attack1 = (int)(Math.random()*10000)%11 + 5;
            attack2 = (int)(Math.random()*10000)%11 + 5;
            //让玩家先攻击
            System.out.println(userName + "打出了一击-里百八式 八酒杯" + comName + "掉血" + attack1);
            hp2 -= attack1;  //玩家攻击,电脑掉血
            System.out.println(comName + "的当前血量为" + hp2);
            Thread.sleep(1000);
            if(hp2 <= 0)
                break;
            System.out.println("\n" + "\n" + "====================================");
            System.out.println(comName + "打出了一击-朝天冲" + userName + "掉血" + attack2);
            hp1 -= attack2;
            System.out.println(userName + "的当前血量为" + hp1);
            System.out.println("\n" + "\n" + "====================================");
            Thread.sleep(1000);
            if( hp1 <= 0)
                break;
        }
        System.out.println("KO");
        if(hp1 > 0){
            System.out.println("恭喜" + userName + "获得了胜利");
        }else if (hp2 > 0){
            System.out.println("恭喜" + comName + "获得了胜利");
        }
        System.out.println(userName + "\t" + hp1);
        System.out.println(comName + "\t" + hp2);
        input.close();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值