2016-04-25 15 views
0

Hallo Ich nehme eine High-School-Klasse und ich brauche Hilfe. Ich bekomme den Fehler im Titel erwähnt. hier ist mein Code (ich soll Zufallszahlen machen für die Menschen zu erraten, wo eine Null ist, und ich muss sie wissen lassen, wenn theyre es und wie nahe theyre schließen)inkompatible Typen: mögliche verlustreiche Konvertierung von Double zu Int (High School hw)

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); 

    } 
} 
+2

Welche Codezeile den Fehler verursacht? –

+0

in der for-Schleife nums [i] = (int) 100 * Math.random(); – jacobOlivares123

+0

Mögliches Duplikat von [Inkompatible Typen: mögliche verlustreiche Konvertierung von Double in Int] (http://stackoverflow.com/questions/29173575/incompatible-types-possible-lossy-conversion-from-double-to-int) –

Antwort

0

Die Math.random wird eine doppelte Rückkehr

so anstatt Gießen zu einem int versuchen

new Double (100*Math.random()).intValue(); 
+0

Danke, das hat funktioniert (ich habe keine Ahnung, was du gerade gemacht hast b/c mein Lehrer hat uns nicht gelehrt .intValue() aber danke nochmal) – jacobOlivares123

+0

Jacob, sieh dir die javadocs für https://docs.oracle.com/ an javase/7/docs/api/overview-summary.html –

+0

@ jacobOlivares123 Wenn Sie mit meiner Antwort zufrieden sind, denken Sie bitte daran zu akzeptieren und/oder upvote - Danke –

Verwandte Themen