| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2873 人关注过本帖, 2 人收藏
标题:求教如何精简编程
取消只看楼主 加入收藏
百分之一
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-16
结帖率:0
收藏(2)
已结贴  问题点数:20 回复次数:1 
求教如何精简编程
  各位大神好,本人新学小白这是我刚学会自己编的一个控制台小游戏,请教大神们该如何编写才能达到符合一般要求的精简程序代码。
public class YZDEL{
    public static void main(String[] args){
        int elHP = 10000;int yzHP = 1000;/*定义恶龙HP10000,勇者HP1000*/
        int elMP = 500;int yzMP = 500;/*定义恶龙MP500,勇者MP500*/
        int elNP = 0;int yzNP = 0;/*定义恶龙怒气0,勇者怒气0*/
        int elnp = 0;int yznp = 0;
        System.out.println("欢迎来到勇者斗恶龙 by百分之一");
        int i = 1;
        int c = 0;
        int d = 0;
        java.util.Scanner s = new java.util.Scanner(System.in);
        for (;elHP > 0 && yzHP > 0;){
            System.out.println("第"+ i +"回合——————————————————————————————————");/*回合计数设置*/
            if (elNP > 100)elNP = 100;/*恶龙怒气值满100点时可使用怒气大招*/
            if (yzNP > 100)yzNP = 100;/*勇者怒气值满100点时可使用怒气大招*/
            if (i % 3 == 0){
                elHP = elHP + 500;
                    if (elHP > 10000)elHP = 10000;/*每3回合恶龙回复500生命值*/
                elMP = elMP + 100;
                    if (elMP > 500)elMP = 500;/*每3回合恶龙回复100魔法值*/
                yzMP = yzMP + 50;
                    if (yzMP > 500)yzMP = 500;/*每3回合勇者回复50魔法值*/
            }
            i++;c++;
            System.out.println("恶龙HP"+ elHP +"        恶龙MP"+ elMP +"       恶龙怒气"+ elNP);
            System.out.println("勇者HP"+ yzHP +"        勇者MP"+ yzMP +"       勇者怒气"+ yzNP);
            System.out.println("1、进攻            2、防御");
            System.out.println("3、逃跑            4、投降");
            System.out.print("选择方式:");
            int a = s.nextInt();
                if (a == 1){
                    System.out.println("1、普通攻击        2、圣光治疗/MP50");
                    System.out.println("3、重斩击/MP100    4、龙灭斩/怒气100");
                    System.out.print("选择技能:");
                    int b = s.nextInt();
                        if (b == 1){
                            elHP = elHP - 500;
                            yznp = yznp + 500;
                            yzNP = yznp / 30;/*勇者每对恶龙造成3000点伤害怒气值加满为100点*/
                            System.out.println("你使用了普通攻击,对恶龙造成了500点伤害");
                            if (elHP <= 0) break;
                            for (;a ==1;) {
                                if (elNP == 100){
                                    System.out.println("恶龙使用了召唤平底锅,对你造成了9999点伤害");/*恶龙怒气大招一次使用消耗100怒气值不防御直接秒杀没商量*/
                                    System.out.println("游戏结束");
                                    yzHP = 0;
                                }else if (c % 3 == 0){
                                    System.out.println("恶龙使用了龙熄,对你造成了500点伤害");/*恶龙魔法伤害一次使用消耗100点魔法值*/
                                    yzHP = yzHP - 500;
                                    elnp = elnp + 500;/*恶龙每对玩家造成1000点伤害怒气值加满为100点*/
                                    elNP = elnp / 10;
                                }else{
                                    System.out.println("恶龙使用了爪击,对你造成了200点伤害");/*恶龙普通伤害不消耗魔法值*/
                                    yzHP = yzHP - 200;   
                                    elnp = elnp + 200;
                                    elNP = elnp / 10;
                                }
                            a = 0;
                        }
                        }else if (b == 2){
                            if (yzMP >= 50){
                                yzHP = yzHP + 500;
                                yzMP = yzMP - 50;
                                System.out.println("你使用了圣光治疗,你回复了500点生命");/*勇者恢复技能一次使用消耗50点魔法值*/
                            }else{
                                System.out.println("你的魔法值不够,无法进行恢复");
                            }
                            for (;a ==1;) {
                                if (elNP == 100){
                                    System.out.println("恶龙使用了召唤平底锅,对你造成了9999点伤害");
                                    System.out.println("游戏结束");
                                    yzHP = 0;
                                }else if (c % 3 == 0){
                                    System.out.println("恶龙使用了龙熄,对你造成了500点伤害");
                                    yzHP = yzHP - 500;
                                    elnp = elnp + 500;
                                    elNP = elnp / 10;
                                }else{
                                    System.out.println("恶龙使用了爪击,对你造成了200点伤害");
                                    yzHP = yzHP - 200;   
                                    elnp = elnp + 200;
                                    elNP = elnp / 10;
                                }
                            a = 0;
                        }
                        }else if (b == 3){
                            if (yzMP >= 100){
                                elHP = elHP - 1000;
                                yzMP = yzMP - 100;
                                yznp = yznp + 500;
                                yzNP = yznp / 30;
                                System.out.println("你使用了重斩击,对恶龙造成了1000点伤害");/*勇者魔法技能一次使用消耗100点魔法值*/
                                if (elHP <= 0) break;
                            }else{
                                System.out.println("你的魔法值不够,无法进行攻击");
                            }
                            for (;a ==1;) {
                                if (elNP == 100){
                                    System.out.println("恶龙使用了召唤平底锅,对你造成了9999点伤害");
                                    System.out.println("游戏结束");
                                    yzHP = 0;
                                }else if (c % 3 == 0){
                                    System.out.println("恶龙使用了龙熄,对你造成了500点伤害");
                                    yzHP = yzHP - 500;
                                    elnp = elnp + 500;
                                    elNP = elnp / 10;
                                }else{
                                    System.out.println("恶龙使用了爪击,对你造成了200点伤害");
                                    yzHP = yzHP - 200;   
                                    elnp = elnp + 200;
                                    elNP = elnp / 10;
                                }
                            a = 0;
                            }
                        }else if (b == 4){
                            if (yzNP == 100){
                                elHP = elHP - 3000;
                                yznp = 0;
                                yzNP = 0;
                                System.out.println("你使用了龙灭斩,对恶龙造成了3000点伤害");/*勇者怒气技能一次使用消耗100点怒气值*/
                                if (elHP <= 0) break;
                            }else{
                                System.out.println("你的怒气值不够,无法进行攻击");
                            }
                            for (;a ==1;) {
                                if (elNP == 100){
                                    System.out.println("恶龙使用了召唤平底锅,对你造成了9999点伤害");
                                    System.out.println("游戏结束");
                                    yzHP = 0;
                                }else if (c % 3 == 0){
                                    System.out.println("恶龙使用了龙熄,对你造成了500点伤害");
                                    yzHP = yzHP - 500;
                                    elnp = elnp + 500;
                                    elNP = elnp / 10;
                                }else{
                                    System.out.println("恶龙使用了爪击,对你造成了200点伤害");
                                    yzHP = yzHP - 200;        
                                    elnp = elnp + 200;
                                    elNP = elnp / 10;
                                }
                            a = 0;
                            }
                        }else {
                            System.out.println("程序出现BUG");
                            System.out.println("游戏结束");
                            break;
                        }
                }else if (a == 2){
                    System.out.println("你选择了防御");
                    for (;a ==2;) {
                                if (elNP == 100){
                                    System.out.println("恶龙使用了召唤平底锅,因为你使用了防御,恶龙对你造成了100点伤害");
                                    yzHP = yzHP - 100;
                                    elnp = 0;
                                    elNP = 0;
                                    c--;/*怒气攻击不计算在2次平A加一次魔法攻击的伤害循环中*/
                                }else if (c % 3 == 0){
                                    System.out.println("恶龙使用了龙熄,因为你使用了防御,恶龙对你造成了50点伤害");
                                    yzHP = yzHP - 50;
                                    elnp = elnp + 50;
                                    elNP = elnp / 10;
                                }else{
                                    System.out.println("恶龙使用了爪击,因为你使用了防御,恶龙对你造成了0点伤害");                    
                                }
                            a = 0;
                        }
                }else if (a == 3){
                    d = a;
                    System.out.println("你选择了逃跑");
                        if (elHP <= 1000){
                            System.out.println("逃跑成功");
                            System.out.println("游戏结束");
                            elHP = 0;
                        }else{
                            System.out.println("逃跑失败");
                            System.out.println("游戏结束");
                            elHP = 0;
                        }
                }else if (a == 4){
                    d = a;
                    System.out.println("你选择了投降");
                    System.out.println("游戏结束");
                    elHP = 0;
                }else{
                    System.out.println("程序出现BUG");
                    System.out.println("游戏结束");
                    elHP = 0;
                }
        }
        if (elHP <= 0 && d == 1)System.out.println("恭喜你打败了恶龙,游戏结束。");
        else if(yzHP <= 0)System.out.println("你被恶龙杀死了,游戏结束。");
    }
}
搜索更多相关主题的帖子: System int println 游戏 out 
2019-09-16 23:22
百分之一
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-16
收藏
得分:0 
回复 2楼 林月儿
我希望知道程序如何写能更短更精简最有效率,如何缩短语句的字数行数,我特别是认为嵌套的恶龙for循环输出语句肯定有更精简的写法,只需要写一遍然后就能套进整个方法中同样运行的写法。因为这个for循环我只是复制了几遍,要是程序需要复制几百遍肯定不能这样写。

[此贴子已经被作者于2019-9-17 01:19编辑过]

2019-09-17 01:12
快速回复:求教如何精简编程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021671 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved