dimanche 14 juin 2020

How to generate random number between (30 - 15n) and (30 +16n) (inclusive)?

I am working on a homework problem that asks me to write a simple program that simulates a fighting game that randomly generates damages for three attacks. The first two simply generate a random number between 28-32 (inclusive). However, the last attack is a charged attack. The user is asked to input the number of seconds they want the attack to be charged for and this number affects the damage output. This charged attack must generate a random number between (30 - 15n) and (30 + 16n) (inclusive) with n being the number of seconds charged. If the user chooses 0 seconds, then the attack will always output 30. If they input 1, then the range would be between 15 and 46 damage. If they input 3, then the range would be between -15 and 78 damage.

I am not sure how to go about writing the charged attack. I am sure its fairly simple but I am new at programming. Could I get some help on how to approach this?

Here is what I have written so far:

    //Variable Declaration
    int atkCharge, atk1, atk2, atk3;

    //Getting user input for how long the attack will be charged for
    System.out.println("Slotherius chooses Triple Claw Slash!");
    System.out.print("How many seconds do you wish to charge the attack?  ");
    atkCharge = scnr.nextInt();

    //Genrating attack damages 
    atk1 = randGen.nextInt(5) + 28;
    atk2 = randGen.nextInt(5) + 28; 
atk3 = //TODO: generate charged attack

System.out.println(atk1); //temp test code
System.out.println(atk2); //^^
System.out.println(atk3);




}

}




Aucun commentaire:

Enregistrer un commentaire