Note that there are some explanatory texts on larger screens.

plurals
  1. POI need help fixing (Printing correct information) a simple app where a player attacks a boss with a random generator
    primarykey
    data
    text
    <p>I made this simple app where a player attacks a boss and the player has to tries to defeat the boss. What I am having trouble with is having my small java app to print after the boss has been defeated. My bosses' health is 5 and I have a random dice generator that ranges 1 - 6. When the player rolls "5" or a "6" on the first attack, I want the program to end. Another thing is that my program prints out I have defeated the boss even though the player has not dealt 5 or 6 of damage. I think it might be because of the for loop but I'm not sure.</p> <p>Here is the code:</p> <pre><code>import java.util.Scanner; import java.util.Random; public class Hey{ public static void main (String args[]){ Scanner attack = new Scanner(System.in); Random dice = new Random(); String hits; int hitss,total = 0; System.out.println("Time to attack the boss!"); System.out.println("Press letter 'A' to attack!"); hits = attack.next(); hitss = 1+dice.nextInt(6); for(int i=0;i&lt;2;i++){ if(hits.equals ("a")){ System.out.println("You have hit a " + hitss); total += hitss; } if(total &gt;= 5){ System.out.println("Well done, You have defeated the boss!"); } else if(total &lt; 5){ total = 5 - total; System.out.println("You need " + total + " more hits to defeat the boss!"); } else { System.out.println("Sorry, you did not defeat the boss"); } } } } </code></pre> <p>Output:</p> <pre><code>You have hit a 2 You need 3 more hits to defeat the boss! You have hit a 2 Well done, You have defeated the boss! </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload