Note that there are some explanatory texts on larger screens.

plurals
  1. POrandom number generator is generating huge negative numbers
    primarykey
    data
    text
    <p>So I am creating a random code that's not necessarily pertinent to anything at this level, but I'm more testing a few ideas. So for some reason my code will work randomly, but for most of the time it's throwing out a random negative number usually in the vicinity of -413796084. I don't know why it's doing this and I am trying to keep the numbers in the vicinity of 0 - 50. I thought I had done it right but obviously I haven't. Also I am relatively new at Java, if that helps explain any mistakes I made.</p> <pre><code>import java.util.Scanner; import java.util.Random; class damagecalc { public static void main(String args[]){ Scanner input = new Scanner(System.in); Random randamage = new Random(); int totaldmg; System.out.println("Will you (a) attack, (s) defend, (d) skip your turn, (f) magic, (i) use an item?"); String dmgString = input.next(); char dmgChar = dmgString.charAt(0); if(dmgChar == 'a'){ for(int finaldmg=50;finaldmg&lt;=50;finaldmg++){ totaldmg = randamage.nextInt(10); } totaldmg = randamage.nextInt(); if(totaldmg &gt;= 50){ System.out.println("You have defeated the monster!"); }else{ System.out.println("Damage Dealt:" + totaldmg); } } } } </code></pre> <p>EDIT------------------------------------------------</p> <p>So I changed my code and fixed some things and now it's just spitting out 0 every time. Maybe now it will be a little easier to figure out. But I definitely need help.</p> <p>This is the new code:</p> <pre><code>import java.util.Scanner; import java.util.Random; class damagecalc { public static void main(String args[]){ Scanner input = new Scanner(System.in); Random randamage = new Random(); int totaldmg; System.out.println("Will you (a) attack, (s) defend, (d) skip your turn, (f) magic, (i) use an item?"); String dmgString = input.next(); char dmgChar = dmgString.charAt(0); if(dmgChar == 'a'){ for(int finaldmg=1;finaldmg&lt;=50;finaldmg++){ } totaldmg = randamage.nextInt(1); if(totaldmg &gt;= 50){ System.out.println("You have defeated the monster!"); }else{ System.out.println("Damage Dealt:" + totaldmg); } } } } </code></pre>
    singulars
    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.
 

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