| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 529 人关注过本帖
标题:求教如何精简编程
只看楼主 加入收藏
百分之一
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-16
结帖率:0
  已结贴   问题点数:20  回复次数:8   
求教如何精简编程
  各位大神好,本人新学小白这是我刚学会自己编的一个控制台小游戏,请教大神们该如何编写才能达到符合一般要求的精简程序代码。
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: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2005
专家分:8923
注 册:2015-3-19
  得分:7 
什么叫一般要求

知命以下无敌
2019-09-17 00:42
百分之一
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-16
  得分:0 
回复 2楼 林月儿
我希望知道程序如何写能更短更精简最有效率,如何缩短语句的字数行数,我特别是认为嵌套的恶龙for循环输出语句肯定有更精简的写法,只需要写一遍然后就能套进整个方法中同样运行的写法。因为这个for循环我只是复制了几遍,要是程序需要复制几百遍肯定不能这样写。

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

2019-09-17 01:12
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2005
专家分:8923
注 册:2015-3-19
  得分:0 
晚上帮你看看

知命以下无敌
2019-09-17 08:45
hhwz
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:87
帖 子:683
专家分:4435
注 册:2011-5-22
  得分:7 
把公用的抽象出来写成一个方法。把整体的构建成一个个对象

比如 用户角色(定义属性 等)
2019-09-17 10:15
y08zh01
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:3
帖 子:4
专家分:27
注 册:2011-4-30
  得分:7 
public class YZDEL {
    public static void main(String[] args) throws IOException {
        System.out.println("欢迎来到勇者斗恶龙 by百分之一");
        Warrior yz = new Warrior();
        Dragon el = new Dragon();
        do {
            Role.addRound();
            int round = Role.getRound();
            System.out.println("——————————————————第" + round + "回合——————————————————————");/*回合计数设置*/
            if (round % 3 == 0) {
                el.addHp(500);/*每3回合恶龙回复500生命值*/
                el.addMp(100);/*每3回合恶龙回复100魔法值*/
                yz.addMp(50);/*每3回合勇者回复50魔法值*/
            }
            System.out.printf("恶龙 HP %-12d MP %-12d 怒气 %-12dn", el.hp, el.mp, el.np);
            System.out.printf("勇者 HP %-12d MP %-12d 怒气 %-12dn", yz.hp, yz.mp, yz.np);
            System.out.println("1、进攻            2、防御");
            System.out.println("3、逃跑            4、投降");
            System.out.print("选择方式:");
            int a = new java.util.Scanner(System.in).nextInt();
            if (a == 1) {
                yz.attack(el);
                if (el.isAlive()) el.attack(yz);
            } else if (a == 2) {
                yz.defense();
                el.attack(yz);
            } else if (a == 3) {
                System.out.println("你选择了逃跑");
                if (el.hp <= 1000) {
                    System.out.println("逃跑成功");
                    System.out.println("游戏结束");
                    return;
                } else {
                    System.out.println("逃跑失败");
                    el.attack(yz);
                }
            } else if (a == 4) {
                System.out.println("你选择了投降");
                System.out.println("游戏结束");
                return;
            } else {
                System.out.println("你选择了发呆");
                el.attack(yz);
            }
        } while (yz.isAlive() && el.isAlive());
        if (yz.isAlive()) {
            System.out.println("恭喜你打败了恶龙,游戏结束。");
        } else {
            System.out.println("你被恶龙杀死了,游戏结束。");
        }
    }
}

abstract class Role {
    int hp;
    final int _hp;
    int mp;
    final int _mp;
    int np;
    private final int _np = 100;
    private       int npRate;
    int npReal;
    private static int round = 0;

    public Role(int _hp, int _mp, int npRate) {
        this.hp = this._hp = _hp;
        this.mp = this._mp = _mp;
        this.npRate = npRate;
        this.np = 0;
    }

    public void addHp(int hp) {
        this.hp += hp;
        if (this.hp > this._hp) this.hp = this._hp;
    }

    public void addMp(int mp) {
        this.mp += mp;
        if (this.mp > this._mp) this.mp = this._mp;
    }

    public void addNp(int np) {
        this.np += np;
        this.npReal = this.np / this.npRate;
        if (this.npReal > this._np) this.npReal = this._np;
    }

    public static int getRound() {
        return round;
    }

    public static void addRound() {
        round++;
    }

    public boolean isAlive() {
        return hp > 0;
    }

    abstract void attack(Role another);

    abstract void attacked(int damage);
}

class Warrior extends Role {
    private boolean defensing = false;

    public Warrior() {
        super(1000, 500, 30);
    }

    @Override
    void attack(Role another) {
        System.out.println("1、普通攻击        2、圣光治疗/MP50");
        System.out.println("3、重斩击/MP100    4、龙灭斩/怒气100");
        System.out.print("选择技能:");
        int b = new java.util.Scanner(System.in).nextInt();
        if (b == 1) {
            System.out.print("你使用了普通攻击");
            another.attacked(500);
            this.addNp(500);
        } else if (b == 2) {
            if (mp >= 50) {
                addHp(500);
                addMp(-50);
                System.out.println("你使用了圣光治疗,你回复了500点生命");/*勇者恢复技能一次使用消耗50点魔法值*/
            } else {
                System.out.println("你的魔法值不够,无法进行恢复");
            }
        } else if (b == 3) {
            if (mp >= 100) {
                System.out.print("你使用了重斩击");
                another.attacked(1000);
                addMp(-100);
                addNp(500);
            } else {
                System.out.println("你的魔法值不够,无法进行攻击");
            }
        } else if (b == 4) {
            if (npReal == 100) {
                System.out.print("你使用了龙灭斩");
                another.attacked(3000);
                clearNp();
            } else {
                System.out.println("你的怒气值不够,无法进行攻击");
            }
        } else {
            System.out.println("输入有误");
            attack(another);
        }
    }

    void minusHp(int damage) {
        hp -= damage;
        if (defensing) {
            System.out.print(",因为你使用了防御");
        }
        System.out.println(",恶龙对你造成了" + damage + "点伤害");
    }

    @Override
    void attacked(int damage) {
        if (defensing) {
            if (damage > 500) {
                minusHp(100);
            } else if (damage == 500) {
                minusHp(50);
            } else {
                minusHp(0);
            }
            defensing = false;
        } else {
            minusHp(damage);
        }
    }

    public void defense() {
        System.out.println("你选择了防御");
        defensing = true;
    }

    void clearNp() {
        np = 0;
        npReal = 0;
    }
}

class Dragon extends Role {
    public Dragon() {
        super(10000, 500, 10);
    }

    @Override
    void attack(Role another) {
        if (npReal == 100) {
            System.out.print("恶龙使用了召唤平底锅");/*恶龙怒气大招一次使用消耗100怒气值不防御直接秒杀没商量*/
            another.attacked(9999);
        } else if (getRound() % 3 == 0 && mp >= 100) {
            System.out.print("恶龙使用了龙熄");/*恶龙魔法伤害一次使用消耗100点魔法值*/
            addNp(500);
            addMp(-100);
            another.attacked(500);
        } else {
            System.out.print("恶龙使用了爪击");/*恶龙普通伤害不消耗魔法值*/
            addNp(200);
            another.attacked(200);
        }
    }

    @Override
    void attacked(int damage) {
        addMp(-damage);
        System.out.println(",对恶龙造成了" + damage + "点伤害");
    }
}
2019-09-17 17:31
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2005
专家分:8923
注 册:2015-3-19
  得分:0 
程序代码:
package com.huawei.test;

import java.util.Scanner;

public class Test0917 {
    private static final String CHOICE_ATTACK = "1"; // 进攻
    private static final String CHOICE_DEFEND = "2"; // 防御
    private static final String CHOICE_ESCAPE = "3"; // 逃跑
    private static final String CHOICE_SURRENDER = "4";    // 投降
    static class Role {
        String name;
        int hp;//血量
        int mp;//魔法值
        int np;//怒气值
        int allnp=0;//怒气总值
        int maxHp;
        int maxMp;
        int recoverBase;//怒气恢复基数
        public Role(String name, int hp, int mp, int np, int recoverBase) {
            super();
            this.name = name;
            this.hp = hp;
            this.mp = mp;
            this.np = np;
            this.maxHp = hp;
            this.maxMp = mp;
            this.recoverBase = recoverBase;
        }
        public void showInfo() {
            System.out.printf("%s[HP:%d\tMP:%d\tNP:%d]\n", name, hp, mp, np);
        }
        public void recover(int hp, int mp) {
            this.hp += hp;
            this.mp += mp;
            if(this.hp > maxHp) {
                this.hp = maxHp;
            }
            if(this.mp > maxMp) {
                this.mp = maxMp;
            }
        }
        public boolean isAlive() {
            return this.hp > 0;
        }
        public void attack(Role target, int hp, int addedNp) {
            target.recover(hp, 0);
            this.allnp += addedNp;
            this.np = this.allnp/recoverBase;
        }
        public boolean isAngry() {
            if(this.np >= 100) {
                this.np -= 100;
                return true;
            }
            return false;
        }
        public boolean hasMagic(int mp) {
            if(this.mp >= mp) {
                this.mp -= mp;
                return true;
            }
            return false;
        }
        public boolean hasHp(int hp) {
            return this.hp>=hp;
        }
    }
    
    public static void main(String[] args) {
        // 名字,生命值,魔法值,怒气值,怒气增加基数
        Role warrior = new Role("勇士", 1000, 500, 0, 30);
        Role dragon = new Role("恶龙", 10000, 500, 0, 10);
        Scanner inputUtil = new Scanner(System.in);
        runAction(warrior, dragon, 1, inputUtil);
    }
    private static final String ATTACK_EASY = "1";
    private static final String ATTACK_CURE = "2";
    private static final String ATTACK_MID = "3";
    private static final String ATTACK_HEAVY = "4";
    static int elnp = 0, yznp = 0;
    private static void runAction(Role warrior, Role dragon, int round, Scanner inputUtil) {
        System.out.println("第"+ round +"回合——————————————————————————————————");
        if(round % 3 == 0) {             
            /*每3回合恶龙回复500生命值,100魔法值*/
            /*每3回合勇者回复50魔法值*/
            dragon.recover(500, 100);
            warrior.recover(0, 50);
        }
        // 
        warrior.showInfo();
        dragon.showInfo();
        System.out.println("1、进攻            2、防御");
        System.out.println("3、逃跑            4、投降");
        String choice = inputUtil.nextLine();
        switch(choice) {
        case CHOICE_ATTACK:
            System.out.println("1、普通攻击        2、圣光治疗/MP50");
            System.out.println("3、重斩击/MP100    4、龙灭斩/怒气100");
            String attackChoice = inputUtil.nextLine();
            switch(attackChoice) {
            case ATTACK_EASY:
                /*勇者每对恶龙造成3000点伤害怒气值加满为100点*/
                System.out.println("你使用了普通攻击,对恶龙造成了500点伤害");
                warrior.attack(dragon, -500, 500);
                break;
            case ATTACK_CURE:
                /*勇者恢复技能一次使用消耗50点魔法值*/
                if(warrior.hasMagic(50)) {
                    System.out.println("你使用了圣光治疗,你回复了500点生命");
                    warrior.recover(500, -50);
                } else {
                    System.out.println("你的魔法值不够,无法进行恢复");
                }
                break;
            case ATTACK_MID:
                /*勇者魔法技能一次使用消耗100点魔法值*/
                if(warrior.hasMagic(100)) {
                    System.out.println("你使用了重斩击,对恶龙造成了1000点伤害");
                    warrior.attack(dragon, -1000, 500);
                }else{
                    System.out.println("你的魔法值不够,无法进行攻击");
                }
                break;
            case ATTACK_HEAVY:
                /*勇者怒气技能一次使用消耗100点怒气*/
                if(warrior.isAngry()) {
                    System.out.println("你使用了龙灭斩,对恶龙造成了3000点伤害");
                    warrior.attack(dragon, -3000, 0);
                }else{
                    System.out.println("你的怒气值不够,无法进行攻击");
                }
                break;
            }
            break;
        case CHOICE_DEFEND:
            System.out.println("你选择了防御");
            break;
        case CHOICE_ESCAPE:
            System.out.println("你选择了逃跑");
            if (!dragon.hasHp(1000)){
                System.out.println("逃跑成功");
                runAction(warrior, dragon, round+1, inputUtil);
                return;
            }else{
                System.out.println("逃跑失败");
            }
            break;
        case CHOICE_SURRENDER:
            System.out.println("你选择了投降");
            System.out.println("游戏结束");
            break;
        }
        if (dragon.isAngry()){
            System.out.println("恶龙使用了召唤平底锅,对你造成了9999点伤害");
            /*恶龙怒气大招一次使用消耗100怒气值不防御直接秒杀没商量*/
            dragon.attack(warrior, -9999, 9999);
            System.out.println("游戏结束");
        } else {
            int cutMidHp = 500;
            int cutCmHp = 200;
            if(CHOICE_DEFEND.equals(choice)) {
                cutMidHp = 50;
                cutCmHp = 0;
            }
            if (round % 3 == 0){
                /*恶龙魔法伤害一次使用消耗100点魔法值*/
                /*恶龙每对玩家造成1000点伤害怒气值加满为100点,防御伤害:50*/
                System.out.println("恶龙使用了龙熄,对你造成了"+cutMidHp+"点伤害");
                dragon.attack(warrior, -cutMidHp, cutMidHp);
            }else{
                /*恶龙普通伤害不消耗魔法值,防御伤害:0*/
                System.out.println("恶龙使用了爪击,对你造成了"+cutCmHp+"点伤害");
                dragon.attack(warrior, -cutCmHp, cutCmHp);
            }
        }
        // 结算
        if(!warrior.isAlive()) {
            if(dragon.isAlive()) {
                System.out.println("你被恶龙杀死了,游戏结束。");
            }else {
                System.out.println("平局,游戏结束。");
            }
        }
        if(!dragon.isAlive()) {
            if(warrior.isAlive()) {
                System.out.println("恭喜你打败了恶龙,游戏结束。");
            } else {
                System.out.println("平局,游戏结束。");
            }
        }
        if(!dragon.isAlive() || !warrior.isAlive()) {
            warrior.showInfo();
            dragon.showInfo();
            inputUtil.close();
            return;
        }
        runAction(warrior, dragon, round+1, inputUtil);
    }
}


[此贴子已经被作者于2019-9-18 11:23编辑过]


知命以下无敌
2019-09-18 07:54
beichei5d
Rank: 4
等 级:业余侠客
威 望:2
帖 子:89
专家分:270
注 册:2016-3-8
  得分:0 
膜拜一下。。。

你现在所偷的懒,都将成为以后扇你的巴掌!共勉吧。。。
2019-09-26 09:02
a363851601
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2019-10-25
  得分:0 
膜拜一下。
2019-10-25 15:51
快速回复:求教如何精简编程
数据加载中...
 
   



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

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