在do-while循环中从Integer中减去

时间:2016-02-25 02:43:20

标签: java

我一般都是编程新手,几天前刚开始使用Java编程,所以我不确定我的程序编码方式是否可以实现我想要实现的目标

我基本上写了一个程序,要求一个"力量"它在0到9之间。它要求你选择一个武器((1)刀是唯一可用的)。它需要这些信息并产生1-3之间的随机伤害比率加上你的力量所在的数字,假设以保持从20减去该值直到它达到0然后退出。

问题在于我陷入了困境。它从20减去了值,但重新开始并再次从20减去而不是存储减去的数字。

有没有人对如何完成这样的事情有任何建议?很感谢任何形式的帮助。到目前为止这是我的代码......

package untitledgame;

import java.util.Random;
import java.util.Scanner;   

public class UntitledGame {


    public static void main(String[] args){

        int pStrength, wKnife, damage, enemyDamage;

        Scanner in = new Scanner(System.in);  //used for next to int inputs

        System.out.println("Choose Your Strength (0-9)");
        pStrength = in.nextInt();
        System.out.println("Your Strength Is "+pStrength);

        System.out.println("Choose Your Weapon; (1)Knife");
        wKnife = in.nextInt();

        System.out.println("Press ENTER to battle...");
        Scanner scanner = new Scanner(System.in);  //waits for users to continue
        scanner.nextLine();



        if (wKnife == 1){


        do {

            int enemyHP = 20;

            Random rn = new Random();  //Random generator
            wKnife = rn.nextInt(3) + 1;  //Randomly generates knife damage


            damage = wKnife + pStrength;  //Damage logic
            System.out.println("Attack with knife has done: " );  //Knife damage     
            System.out.print(+damage);
            System.out.print(" damage." );

            System.out.println("");

            enemyDamage = enemyHP - damage;  // Remaing HP 

            System.out.println("Enemy has ");
            System.out.print(+enemyDamage);
            System.out.print(" HP left.");

            System.out.println("");

            System.out.println("Press Enter to Attack...");         
            scanner.nextLine();  

        } 
            while (enemyDamage > 0);

            System.out.println("Enemy has been defeated.");


        }

    }

}

3 个答案:

答案 0 :(得分:2)

int enemyHP = 20;循环之外声明此do-while。现在,您每次迭代循环时都会将enemyHP重置为20。

此外,您不应在Random循环内重新创建do-while。在进入do块之前执行此操作。

Jonah Haney在答案中处理的逻辑存在其他问题。

答案 1 :(得分:0)

在循环开始之前声明int enemyHP = 20。然后您的循环缺少以下行:

enemyHP = enemyHP - damage;

此外,似乎你希望循环继续运行直到敌人被击败。如果是这种情况,那么您的情况应该检查enemyHP是否已经完全耗尽,如下所示:

}
    while (enemyHP > 0);    //instead of while (enemyDamage > 0);

修改

这就是你的代码应该是什么样子。我不是说你应该把它直接复制到你的项目中。我说你应该看一下差异,它会帮助你看到自己代码中的逻辑问题:

public static void main(String[] args){

    int pStrength, wKnife, damage, enemyDamage;

    Scanner in = new Scanner(System.in);  //used for next to int inputs

    System.out.println("Choose Your Strength (0-9)");
    pStrength = in.nextInt();
    System.out.println("Your Strength Is " + pStrength);

    System.out.println("Choose Your Weapon; (1)Knife");
    wKnife = in.nextInt();

    System.out.println("Press ENTER to battle...");
    in.nextLine();
    in.nextLine();

    Random rn = new Random();  //Random generator
    int enemyHP = 20;

    if (wKnife == 1){

        do {
            System.out.println("Press Enter to Attack...");         
            in.nextLine();  

            wKnife = rn.nextInt(3) + 1;   //Randomly generates knife damage
            damage = wKnife + pStrength;  //Damage logic

            System.out.println("Attack with knife has done: " + damage + " damage.");  //Knife damage

            enemyHP -= damage;            // Calculate Remaining HP 
            System.out.println("Enemy has " + enemyHP + " HP left.");

        } while (enemyHP > 0);

        System.out.println("Enemy has been defeated.");
        in.close();
    }
}

答案 2 :(得分:0)

你需要初始化do-while循环的enemyHP变量OUTSIDE,否则它会在每次循环时将enemyHP重置为20。