0

Hello I am taking a high school class and I need help. I am getting the error mentioned in the title. here is my code (i am supposed to make random numbers for people to guess where a zero is and i need to let them know if theyre close to it and how close theyre)

    import javax.swing.JOptionPane;
    public class p5g
    {
        public static void main(String[] arg)
        {
            String width = JOptionPane.showInputDialog("How many rows do you     want?");        
            String length = JOptionPane.showInputDialog("How many columns do you want?");
        int lol = Integer.parseInt( width );
        int wow = Integer.parseInt( length );
        int[][]gameBoard = new int[lol][wow];

        int[] nums = new int[lol*wow];
        for(int i =0; i < nums.length; i++)
        {
             nums[i]=(int)100*Math.random();
        }

        String row = JOptionPane.showInputDialog("Choose a row");        
        String col = JOptionPane.showInputDialog("Choose a column");
        int ro = Integer.parseInt( row );
        int co = Integer.parseInt( col );

    }
}
Priya
  • 1,359
  • 6
  • 21
  • 41
  • 2
    What line of code is causing the error? – Jonny Henly Apr 25 '16 at 04:45
  • in the for loop nums[i]=(int)100*Math.random(); – jacobOlivares123 Apr 25 '16 at 04:49
  • Possible duplicate of [Incompatible types: possible lossy conversion from double to int](http://stackoverflow.com/questions/29173575/incompatible-types-possible-lossy-conversion-from-double-to-int) – Jonny Henly Apr 25 '16 at 04:50
  • Why not using the Random class itself? It has a #nextInt method. Also, if you want to include boundaries, check out this post: http://stackoverflow.com/questions/5271598/java-generate-random-number-between-two-given-values. – n247s Apr 25 '16 at 05:05

1 Answers1

1

The Math.random will return a double

so rather than casting to an int try

new Double (100*Math.random()).intValue ();
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64