基于对象文本的Java游戏帮助(初学者)

基于对象文本的Java游戏帮助(初学者),java,class,oop,while-loop,this,Java,Class,Oop,While Loop,This,所以我必须创建一个Java游戏。我对Java非常陌生,我整天都在做这个。但我不知道什么是不正确的。这是我的主课,在那里玩游戏。我还有一个角色类,一个武器类和一个装甲类。我现在的问题是,我不希望任何玩家的hp低于0。如果它降到0以下,它应该被改为0,然后那个敌人将不再被选择战斗。我觉得我选择惠普的方式有点错误,但我真的不明白怎么选。有人能帮忙吗 import Character.Character; import java.util.Random; public class Main { p

所以我必须创建一个Java游戏。我对Java非常陌生,我整天都在做这个。但我不知道什么是不正确的。这是我的主课,在那里玩游戏。我还有一个角色类,一个武器类和一个装甲类。我现在的问题是,我不希望任何玩家的hp低于0。如果它降到0以下,它应该被改为0,然后那个敌人将不再被选择战斗。我觉得我选择惠普的方式有点错误,但我真的不明白怎么选。有人能帮忙吗


import Character.Character;

import java.util.Random;
public class Main {

public static void main(String[] args) {    
    Scanner scanner = new Scanner(System.in);
    System.out.println("> Welcome to The Game Project <");
    System.out.println("\n >> Input Main Character Name: ");
    String main_name = scanner.nextLine();
    System.out.println(">> Input Main Character Power: ");
    int main_power=scanner.nextInt();
    System.out.println(">> Input Main Character Hp: ");
    int main_hp=scanner.nextInt();
    System.out.println("----------------------------------------------------");

    Character main_ch=new Character (main_hp,main_power,main_name);
    show_status(main_ch);
    check_bag(main_ch);

    Character enemies[]=new Character [10];
    enemies[0]= new Character("Werewolf");
    enemies[1]= new Character("Vampire");
    enemies[2]= new Character("Alien");
    enemies[3]= new Character("Witch");
    enemies[4]= new Character("Ghost");
    enemies[5]= new Character("Skeleton");
    enemies[6]= new Character("Zombie");
    enemies[7]= new Character("Demon");
    enemies[8]= new Character("Mummy");
    enemies[9]= new Character("Dragon");

    boolean check = false;
GAME:   
    while(true) {

        Random rnd=new Random();
        int selected = rnd.nextInt(enemies.length); //random enemy selected
        enemies[selected].damage(main_ch.hit_point());

        System.out.println();
        System.out.println(">>>>> An enemy has appeared and wants to fight! <<<<<");
        show_status(enemies[selected]);
        System.out.println();
        System.out.println(">> What do you want to do?");
        System.out.println("\t1. Fight!");
        System.out.println("\t2. Run!");
        int input = scanner.nextInt();

        if(input==1) {

            int damageDealt = rnd.nextInt(main_ch.hit_point());
            int damageTaken = rnd.nextInt(main_ch.damage(main_ch.hit_point()));
            enemies[selected].hp -= damageDealt;
            main_ch.hp -= damageTaken;
            System.out.println("You caused "+ damageDealt +" damage to the enemy!");
            System.out.println("You received "+ damageTaken +" damage from the enemy!");
  //        show_status(enemies[selected]);

            if(main_ch.hp ==0) {
                System.out.println();
                System.out.println("\t Oh no! You died! Better luck next time. Thanks for playing!");
                System.out.println();
                break;
                }

            int dead_count=0;
            for(int i=0; i<enemies.length;i++) {
            if(enemies[i].getHp()==0) {
                dead_count=dead_count+1;
            }
            }
            if(dead_count==enemies.length) {
                check=true;
            }
            if(check) {
                System.out.println(">>>>> You won! Congratulations, you defeated all of your enemies! <<<<<");
                break;
           }


        }
        else if(input==2) {
            System.out.println();
            System.out.println("-----You ran away to safety! .... But, uh oh... What is that over there?-----");
                continue GAME;
        }
        else {
            System.out.println(">>>>>>>>>> Your entry is invalid. <<<<<<<<<<");
        }                        

    }

}
public static void show_status(Character character) {
    System.out.println("----------------- Character Status -----------------");
    System.out.println("\tCharacter Name:\t\t"+character.getName());
    System.out.println("\tCharacter HP:\t\t"+character.getHp());
    System.out.println("\tCharacter Power:\t"+character.getPower());
    System.out.println("\tCharacter Defense:\t"+character.getDefense());
    System.out.println("\tCharacter MP:\t\t"+character.getMp());
    System.out.println("\tCharacter Level:\t"+character.getLevel());
    System.out.println("\tWeapon Name:\t\t"+character.getWeapon().getName());
    System.out.println("\tWeapon Power:\t\t"+character.getWeapon().getPower());
    System.out.println("\tArmor Name:\t\t"+character.getArmor().getName());
    System.out.println("\tArmor Defense:\t\t"+character.getArmor().getDefense());
    System.out.println("----------------------------------------------------");
}
public static void check_bag(Character character) {
    System.out.println("-------------------- Bag Status --------------------");
    System.out.println("\tMoney:\t\t\t$"+ character.getBag().getMoney());
    for(int i = 0; i<4; i++) {
        System.out.println("\tWeapon Name/Power:\t"+ character.getBag().getWeaponArray()[i].getName()+" // "+character.getBag().getWeaponArray()[i].getPower());
    }
    for(int i = 0; i<4; i++) {
        System.out.println("\tArmor  Name/Defense:\t"+ character.getBag().getArmorArray()[i].getName()+" // "+character.getBag().getArmorArray()[i].getDefense());
    }
    System.out.println("----------------------------------------------------");
}```



    This is the code for the Character package:

    ```package Character;
    import java.util.Random;
    import Equipment.*;

    public class Character {
    private Armor armor = new Armor();
    private Weapon weapon = new Weapon();
    private Bag bag = new Bag();

    public int hp, power, defense, mp, level;
    private String name;
    Random rnd=new Random(); 

    public Character(String name) {
        this.name=name;
        Random rnd=new Random();
        this.hp=rnd.nextInt(1000)+1;
        this.power=rnd.nextInt(100)+1;
        this.defense=rnd.nextInt(10)+1;
        this.mp=rnd.nextInt(100)+1;
        this.level=1;
}
    public Character(int hp, int power, String name) {
        this.hp=hp;
        this.power=power;
        this.name=name;
        this.defense=rnd.nextInt(10)+1;
        this.mp=rnd.nextInt(100)+1;
        this.level=1;
}
    public int getHp() {
        return hp;
}
    public void setHp(int hp) {
        this.hp = hp;
}
    public int getPower() {
        return power;
}
    public void setPower(int power) {
        this.power = power;
}
    public int getDefense() {
        return defense;
}
    public void setDefense(int defense) {
        this.defense = defense;
}
    public int getMp() {
        return mp;
}
    public void setMp(int mp) {
        this.mp = mp;
}
    public int getLevel() {
        return level;
}
    public void setLevel(int level) {
        this.level = level;
        if(this.hp==0) {
            this.level = this.level + 1;
        }
    }

    public String getName() {
        return name;
}
    public int damage(int enemy_power) {
        int damage = enemy_power - this.defense;
        if(damage<0){ //avoid healing by damage
            damage=0;
}
        this.hp=this.hp - damage;
        if(this.hp<0) { //avoid negative hp
            this.hp = 0;
}
        return damage;
}

    public Armor getArmor() {
    return armor;
}
    public void setArmor(Armor armor) {
        this.armor = armor;
}
    public Weapon getWeapon() {
        return weapon;
}
    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
}

    public int hit_point() {
        int total_power = this.power + this.weapon.getPower();
        return total_power;
    }

    public Bag getBag() {
        return bag;
    }
    public void setBag(Bag bag) {
        this.bag = bag;
    }

    public class Bag{
        Weapon weaponArray[] = new Weapon[4];
        Armor armorArray[] = new Armor[4];
        int money = 150;
        public Bag(){
            for(int i=0; i<weaponArray.length; i++) {
                weaponArray[i] = new Weapon();
                armorArray[i] = new Armor();
            }
        }
        public Weapon[] getWeaponArray() {
            return weaponArray;
        }
        public void setWeaponArray(int yourWeaponIndex, Weapon enemyWeapon) {
            this.weaponArray[yourWeaponIndex] = enemyWeapon;
        }
        public Armor[] getArmorArray() {
            return armorArray;
        }
        public void setArmorArray(Armor[] armorArray) {
            this.armorArray = armorArray;
        }
        public int getMoney() {
            return money;
        }
        public void setMoney(int money) {
            this.money = money;
        }

    }
}```

I will also have to figure out a few more things like how to increase the main character's level whenever they kill an enemy. increase the hit_point and decreased the mp if a skill is used (by the way, does anyone know what mp stands for?), and switch out weapons and armor with a dead enemy if theirs is better than mine. 

输入字符。字符;
导入java.util.Random;
公共班机{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“>欢迎来到游戏项目>输入主要角色名称:”);
字符串main_name=scanner.nextLine();
System.out.println(“>>输入主字符Power:”);
int main_power=scanner.nextInt();
System.out.println(“>>输入主字符Hp:”);
int main_hp=scanner.nextInt();
System.out.println(“------------------------------------------------------------”);
Character main\u ch=新字符(main\u hp、main\u power、main\u name);
显示状态(主通道);
检查行李(主通道);
角色敌人[]=新角色[10];
敌人[0]=新角色(“狼人”);
敌人[1]=新角色(“吸血鬼”);
敌人[2]=新角色(“外星人”);
敌人[3]=新角色(“女巫”);
敌人[4]=新角色(“幽灵”);
敌人[5]=新角色(“骷髅”);
敌人[6]=新角色(“僵尸”);
敌人[7]=新角色(“恶魔”);
敌人[8]=新角色(“木乃伊”);
敌人[9]=新角色(“龙”);
布尔检查=假;
游戏:
while(true){
随机rnd=新随机();
int selected=rnd.nextInt(敌人.长度);//随机选择敌人
敌人[被选中]。伤害(主命中点());
System.out.println();

System.out.println(“>>>>>一个敌人出现并想要战斗!>>你赢了!祝贺你,你打败了所有的敌人!>>>>你的输入无效。这就是你的问题所在:

if(main_ch.hp ==0)
在对角色施加伤害后,你要检查角色的生命值是否为零。如果角色受到一次命中并留下1点或更多的生命,那么战斗可以继续。如果角色受到一次命中并留下少于0点的生命,战斗仍然可以继续。只有当角色受到的伤害将生命值精确降低到0时,战斗才能继续作品

幸运的是,您已经描述了修复:

如果低于0,则应将其更改为0

可以这样完成:

//将条件从零更改为小于或等于零

if(main_ch.hp <= 0) {

    // Added code to reset health

    main_ch.setHp(0);

    System.out.println();
    System.out.println("\t Oh no! You died! Better luck next time. Thanks for playing!");
    System.out.println();
    break;
}

我想你可以猜出如何根据游戏条件和玩家选择来更改条件,从而控制游戏流程,而不必使用
继续
中断

来来回跳跃。谢谢你的帮助!我还以为Characters类中的这段代码已经将0以下的任何值转换为0:公共智力伤害(智力敌方力量){智力伤害=敌方力量-this.defence;if(damageYeah)。你那里的代码似乎将伤害值设置为零,而不是角色的生命值。它说“如果造成的伤害小于零,将伤害设为零。”你真正想要的是如果角色的健康度小于零,则将角色的健康度设为零”。无论如何,如果这个答案对您有所帮助,请单击左侧的复选标记,让未来的读者知道什么对您有效。如果出现问题,请随时发布更多问题。
while(playerWantsToPlay)
{
    // Oh noes! An encounter. Fight or run away bravely?

    while(combatInProgress)
    {
        if(characterIsAlive && targetIsAlive)
        {
            // bash each other in the face
        }
    }
}