Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';I don’我不明白为什么我的电脑坏了_Java - Fatal编程技术网

Java Can';I don’我不明白为什么我的电脑坏了

Java Can';I don’我不明白为什么我的电脑坏了,java,Java,我正在尝试制作一个非常简单的游戏(即使没有界面)。你应该输入你想做的动作(攻击或阻挡)。但是为什么我的(敌方hp>0 | | hp>0)语句不起作用?循环将永远持续下去 public class Game { public static void main(String[] args){ int hp = 10, enemy_hp = 10; String attack = "attack"; String block = "block"

我正在尝试制作一个非常简单的游戏(即使没有界面)。你应该输入你想做的动作(攻击或阻挡)。但是为什么我的(
敌方hp>0 | | hp>0
)语句不起作用?循环将永远持续下去

public class Game {
    public static void main(String[] args){ 
        int hp = 10, enemy_hp = 10;
        String attack = "attack";
        String block = "block";
        Scanner userInput = new Scanner (System.in);
        while (enemy_hp > 0 || hp > 0) { 
            System.out.println("It is your turn, attack or try to block");
            int your_block_chance1 = (int) (Math.random() * 4); //Chance to block an attack
            int enemy_block_chance1 = (int) (Math.random() * 4);
            String action = userInput.next(); 
            if (action.equals(attack)){
                System.out.print("You attacked your enemy and ");
                if (enemy_block_chance1 == 0) {
                    System.out.println("he blocked it");
                }
                else if (enemy_block_chance1 != 0){
                    enemy_hp = enemy_hp - 2;
                    System.out.println("managed to hit, now his hp is " +enemy_hp);
                }
            }       
            else if (action.equals(block)){
                System.out.println("You dicided to block");
                your_block_chance1 = 0; 
            }
            System.out.print("It is your enemy turn, he decided to ");
            int enemy_action = (int) (Math.random() * 2);
            if (enemy_action == 1){
                System.out.print("attack you,");
                if (your_block_chance1 == 0){
                    System.out.println(" but you blocked it");
                }
                else if (your_block_chance1 != 0){
                    hp = hp - 2;
                    System.out.println(" and you didn't block it, now your hp is " +hp);

                }
            }
            else if (enemy_action != 1){
                System.out.print("do a heavy attack");
                int heavy_attack_chance = (int) (Math.random() * 2);
                if (heavy_attack_chance == 1){
                    System.out.println(" but failed");
                }
                else if (heavy_attack_chance != 1){
                    if (your_block_chance1 == 0){
                        System.out.println(" but you blocked it");
                    }
                    else if (your_block_chance1 != 0){
                        hp = hp - 4;
                        System.out.println(" and he managed to hit you really hard, now your hp is " +hp);

                    }
                }
            }
        }
        if (hp <= 0){
            System.out.println("You failed");
        }
        else if (enemy_hp <= 0){
            System.out.println("You won!");
        }
    }
}
公共类游戏{
公共静态void main(字符串[]args){
智力生命=10,敌人生命=10;
字符串攻击=“攻击”;
String block=“block”;
扫描仪用户输入=新扫描仪(System.in);
而(敌人生命>0 | |生命>0){
System.out.println(“轮到你了,攻击或试图阻挡”);
int您的_block_chance1=(int)(Math.random()*4);//阻止攻击的机会
int buck_chance1=(int)(Math.random()*4);
String action=userInput.next();
if(动作等于(攻击)){
System.out.print(“你攻击了你的敌人并”);
如果(敌方阻塞信道1==0){
System.out.println(“他阻止了它”);
}
否则,如果(敌人阻塞机会1!=0){
敌方生命=敌方生命-2;
System.out.println(“成功命中,现在他的生命值为”+敌人生命值);
}
}       
else if(动作等于(块)){
System.out.println(“您决定阻止”);
您的_block_chance1=0;
}
System.out.print(“轮到你的敌人了,他决定了”);
int敌人行动=(int)(Math.random()*2);
如果(敌方行动==1){
系统输出打印(“攻击你,”);
如果(你的区块1==0){
System.out.println(“但您阻止了它”);
}
否则,如果(您的\u块\u机会1!=0){
hp=hp-2;
System.out.println(“您没有阻止它,现在您的hp为”+hp);
}
}
否则如果(敌人行动!=1){
系统输出打印(“进行猛烈攻击”);
int重攻击概率=(int)(Math.random()*2);
如果(重击概率==1){
System.out.println(“但失败”);
}
否则如果(重击概率!=1){
如果(你的区块1==0){
System.out.println(“但您阻止了它”);
}
否则,如果(您的\u块\u机会1!=0){
hp=hp-4;
System.out.println(“他真的狠狠地打了你,现在你的生命是”+hp);
}
}
}
}

如果(hp你希望当一名玩家死亡时while停止吗?那么你应该放一个&。因为除非两名玩家的hp低于0,否则| |永远不会为假。

如果你想在敌人或玩家的生命值降到0或以下时停止,那么你希望你的循环条件使用逻辑AND运算符r而不是或:

 while (enemy_hp > 0 && hp > 0) {

当两者都高于零时,循环将继续。因此,当其中一个(或两者)为零或低于零时,循环将停止。

您的条件使用
|
,或。这意味着您的循环将在敌方HP为正或玩家HP为正时继续(意味着在敌方HP达到0后循环将继续)。将其改为
&&