【问题标题】:Calling an Integer into a Boolean将整数调用为布尔值
【发布时间】:2016-05-01 05:43:51
【问题描述】:

我的代码部分不起作用

}else if(bossOne == 3){
        int bossOneHP = 70 + bossStat.nextInt(10)-5;
        int bossOneOP = 27 + bossStat.nextInt(10)-5;
        int bossOneBP = 12 + bossStat.nextInt(10)-5;
        String bossName = "Spartan King";
        waits();
        System.out.println("Your first enemy is the Spartan King.");
        System.out.println("It has a very powerful attack, lets hope you have enough health.");
    }

    boolean keepPlaying = true;
    while (keepPlaying){
        Scanner choice = new Scanner(System.in);
        System.out.println("Enter 1 to attack.");
        System.out.println("Enter 2 to block.");
        System.out.println("Enter 3 to exit the game.");
        int selection = choice.nextInt();
        if (selection == 1){
            waits();
            System.out.println("You attack.");
            System.out.println(bossOneHP == bossOneHP - (OP - bossOneBP));
        }else if(selection == 2){
            waits();
            System.out.println("You block.");
            System.out.println(HP == HP - (bossOneOP - BP));
        }else if(selection == 3){
            break;
        }
        if (bossHP == (0 || >0){
            System.out.println("Congratulations you won!");
            break;
        }
        if (HP == (0 || >0)){
            System.out.println("Sorry you lost.");
            break;
        }

我需要将整数调用到该部分。这段代码是我为我的编程课程创建的游戏的核心,如果有任何帮助,我们将不胜感激。

【问题讨论】:

标签: java while-loop integer boolean


【解决方案1】:

您想要“大于或等于”运算符:>=

if (bossHP >= 0){
    System.out.println("Congratulations you won!");
    break;
}
if (HP >= 0){
    System.out.println("Sorry you lost.");
    break;
}

【讨论】:

    【解决方案2】:

    好的,所以我不确定应该做什么,但我会试一试。看来你有点糊涂了。

    例如:

        if (selection == 1){
            waits();
            System.out.println("You attack.");
            System.out.println(bossOneHP == bossOneHP - (OP - bossOneBP));
        }else if(selection == 2){
    

    在此代码中,您似乎正在打印以控制台变量 bossOneHP 和 bossOneHP - (OP - bossOneBP) 的比较,我认为这不是您想要的,因为只有 (OP-bossOneBP) 是零(基本代数)。我怀疑你的意图:

        if (selection == 1){
            waits();
            System.out.println("You attack.");
            bossOneHP = bossOneHP - (OP - bossOneBP);
        }else if(selection == 2){
    

    这会将变量 bossOneHP 设置为自身减去(OP 减去 bossOneBP)。请注意,您也可以这样做:

    if (selection == 1){
            waits();
            System.out.println("You attack.");
            bossOneHP-= OP - bossOneBP;
        }else if(selection == 2){
    

    哪个更快。 -= 将值设置为自身减去以下值,而不是 = 将其设置为新值。如果它们相等, == 也会进行比较返回 true 而 = 将变量设置为新值。

    第二期:

    if (bossHP == (0 || >0){
    

    我假设如果 bossHP 小于或等于零,您想激活 if 语句。 || statement 是一个布尔运算符(它比较两侧的两个布尔输入,无论是变量还是比较或函数,如果任一输入为真,则返回单个布尔值 true),并且不像单词那样起作用要么。要比较两个数字(在本例中为变量 bossHP 和零),您可以使用多个运算符之一。它们如下:

    ==  -returns true (which activates the if statement) if the numbers or objects (if they are the same instance, not if they contain equal values) on both sides are identical. 
    
    <  -returns true if the left hand number is smaller than the right hand one (doesn't work on objects)
    
    >  -returns true if the right hand number is smaller
    
    <= -returns true if the left hand number is smaller or equal to the right hand number
    
    >= -returns true if the right hand number is smaller or equal to the left hand number
    
    != -returns true if the numbers or objects do not equal each other (effective opposite of the == token)
    
    !  -only takes one boolean on the right hand side and returns its opposite (this inverts the value essentially), if(!val) is equivalent and better to if(val == false)
    

    正确的代码可能是这样的:

        if (bossHP >= 0){
            System.out.println("Congratulations you won!");
            break;
        }
    

    除了执行 while(keepPlaying) 之外,您还可以执行 while(true) 并休息一下;输入 3 时的命令

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-03
      • 2018-10-22
      • 2017-01-26
      • 2010-12-16
      • 2014-09-03
      • 1970-01-01
      • 2021-03-24
      • 2017-05-04
      相关资源
      最近更新 更多