栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA循环嵌套+分支结构游戏---(以《三国演义》中三英大战吕布为例)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA循环嵌套+分支结构游戏---(以《三国演义》中三英大战吕布为例)

我们做一个java基础学习中关于循环嵌套加分支语句综合应用的破解小游戏--三英大战吕布为例代码如下:
package assignment;

import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name_boss = "吕布", name_npc1 = "刘备", name_npc2 = "关羽", name_npc3 = "张飞";

        int boss_ad = 1000, npc1_ad = 200, npc2_ad = 200, npc3_ad = 200;
        int boss_hp = 5000, np1_hp = 800, np2_hp = 550, np3_hp = 600;
        System.out.println("《《《《《《欢迎来到三英大战吕布(破解版)》》》》》》");
        //展示BOSS和武将的信息属性
        for (int i = 0; boss_hp > 0; i++) {

            System.out.println("---------------------------------------------");
            System.out.println("BOSS【"+name_boss+"】信息:");
            System.out.println("BOSS姓名ttBOSS血量ttBOSS攻击力");
            System.out.println(name_boss + "ttt" + boss_hp + "ttt" + boss_ad);
            System.out.println("---------------------------------------------");
            System.out.println("武将信息:");
            System.out.println("武将姓名tt武将血量tt武将攻击力");
            System.out.println(name_npc1+"ttt"+(np1_hp<=0?"已阵亡":np1_hp)+"ttt"+npc1_ad);
            System.out.println(name_npc2+"ttt"+(np2_hp<=0?"已阵亡":np2_hp)+"ttt"+npc2_ad);
            System.out.println(name_npc3+"ttt"+(np3_hp<=0?"已阵亡":np3_hp)+"ttt"+npc3_ad);

            if (i==0) {
            System.out.println("---------------------------------------------");
            System.out.println("武器信息:");
            System.out.println("雌雄双股剑(刘备专属)   +300 攻击力");
            System.out.println("青龙偃月刀(关羽专属)   +450 攻击力");
            System.out.println("八丈蛇矛 (张飞专属)    +280 攻击力");

            int ad_jian=300;
            int ad_dao=450;
            int ad_mao=280;
            System.out.println("欢迎体验三英战吕布,是否装备武器");
            String s3 = input.next();

            if (s3.equals("y")||s3.equals("Y")) {
                    npc1_ad = npc1_ad + ad_jian;
                    npc2_ad = npc2_ad + ad_dao;
                    npc3_ad = npc3_ad + ad_mao;

                    System.out.println("---------------------------------------------");
                    System.out.println("武将信息:");
                    System.out.println("武将姓名tt武将血量tt武将攻击力");
                    System.out.println(name_npc1+"ttt"+np1_hp+"ttt"+npc1_ad);
                    System.out.println(name_npc2+"ttt"+np2_hp+"ttt"+npc2_ad);
                    System.out.println(name_npc3+"ttt"+np3_hp+"ttt"+npc3_ad);
                }else System.out.println("您未装备任何武器,请进行继续游戏");
            }
            //如果武将已阵亡,会去掉名单
            if (np1_hp > 0) {
                System.out.println("1、" + name_npc1);
            }
            if (np2_hp > 0) {
                System.out.println("2、" + name_npc2);
            }
            if (np3_hp > 0) {
                System.out.println("3、" + name_npc3);
            }

            System.out.println("请选择武将出战:");
            int chooseRole = input.nextInt();
            if (chooseRole>=4){
                    System.err.println("武将无法出战(武将不存在或已阵亡)!!!!");
                    break;
            }
            switch (chooseRole) {

                case 1:
                    //判断武将是否存活
                    if (np1_hp <= 0) {
                        System.out.println(name_npc1 + "已阵亡,无法出战,请重新挑选!");
                        break;
                    }
                    System.out.println("武将【"+name_npc1+"】向 BOSS【"+name_boss+"】 发起了攻击"+"造成了【"+npc1_ad+"】伤害");
                    boss_hp -= npc1_ad;
                    System.out.println();
                    System.out.println("BOSS【"+name_boss+"】 剩余"+ (boss_hp<=0?0:boss_hp)+"血量");
                    System.out.println();

                    if (boss_hp <= 0) {
                        System.out.println("武将获得了胜利!");
                        return;
                    }

                    System.out.println("BOSS【"+name_boss+"】向 武将【"+name_npc1+"】 发起了攻击"+"造成了【"+boss_ad+"】伤害");
                    System.out.println();
                    np1_hp -= boss_ad;
                    System.out.println("武将【"+name_npc1+"】剩余"+(np1_hp<=0?0:np1_hp)+"血量");
                    System.out.println();

                    if (np1_hp <= 0 && np2_hp <= 0 && np3_hp <= 0) {
                        System.out.println("所有武将已阵亡,是否进行破解");
                        String s = input.next();
                        if (s.equals("y")||s.equals("Y")) {
                            boss_hp=20000;
                            np1_hp = 9999;
                            npc1_ad=9999;
                            np2_hp = 9999;
                            npc2_ad=9999;
                            np3_hp = 9999;
                            npc3_ad=9999;
                            break;
                        } else {
                            System.err.println(" GAME OVER....");
                            return;
                        }
                    }
                    break;
                case 2:
                    //判断武将是否存活
                    if (np2_hp <= 0) {
                        System.out.println(name_npc2 + "已阵亡,无法出战,请重新挑选!");
                        break;
                    }
                    System.out.println("武将【"+name_npc2+"】向 BOSS【"+name_boss+"】 发起了攻击"+"造成了【"+npc2_ad+"】伤害");
                    System.out.println();
                    boss_hp -= npc2_ad;
                    System.out.println("BOSS【"+name_boss+"】 剩余"+(boss_hp<=0?0:boss_hp)+"血量");
                    System.out.println();

                    if (boss_hp <= 0) {
                        System.out.println("武将获得了胜利!");
                        return;
                    }
                    System.out.println("BOSS【"+name_boss+"】向 武将【"+name_npc2+"】 发起了攻击"+"造成了【"+boss_ad+"】伤害");
                    System.out.println();
                    np2_hp -= boss_ad;
                    System.out.println("武将【"+name_npc2+"】剩余"+(np2_hp<=0?0:np2_hp)+"血量");
                    System.out.println();

                    if (np2_hp <= 0 && np1_hp <= 0 && np3_hp <= 0) {
                        System.out.println("所有武将已阵亡,是否进行破解");
                        String s1 = input.next();
                        if (s1.equals("y")||s1.equals("Y")) {
                            boss_hp=20000;
                            np1_hp = 9999;
                            npc1_ad=9999;
                            np2_hp = 9999;
                            npc2_ad=9999;
                            np3_hp = 9999;
                            npc3_ad=9999;
                            break;
                        } else {
                            System.err.println(" GAME OVER....");
                            return;
                        }
                    }
                    break;
                case 3:
                    //判断武将是否存活
                    if (np3_hp <= 0) {
                        System.out.println(name_npc3 + "已阵亡,无法出战,请重新挑选!");
                        break;
                    }
                    System.out.println("武将【"+name_npc3+"】向 BOSS【"+name_boss+"】 发起了攻击"+"造成了【"+npc3_ad+"】伤害");
                    System.out.println();
                    boss_hp -= npc3_ad;
                    System.out.println("BOSS【"+name_boss+"】 剩余"+(boss_hp<=0?0:boss_hp)+"血量");
                    System.out.println();

                    if (boss_hp <= 0) {
                        System.out.println("武将获得了胜利!");
                        return;
                    }

                    System.out.println("BOSS【"+name_boss+"】向 武将【"+name_npc3+"】 发起了攻击"+"造成了【"+boss_ad+"】伤害");
                    System.out.println();
                    np3_hp -= boss_ad;
                    System.out.println("武将【"+name_npc3+"】剩余"+(np3_hp<=0?0:np3_hp)+"血量");
                    System.out.println();

                    if (np2_hp <= 0 && np1_hp <= 0 && np3_hp <= 0) {
                        System.out.println("所有武将已阵亡,是否进行破解");
                        String s2 = input.next();
                        if (s2.equals("y")||s2.equals("Y")) {
                            boss_hp=20000;
                            np1_hp = 9999;
                            npc1_ad=9999;
                            np2_hp = 9999;
                            npc2_ad=9999;
                            np3_hp = 9999;
                            npc3_ad=9999;
                            break;
                        }
                    else {
                         System.err.println(" GAME OVER....");
                         return;
                    }
                }
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978491.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号