• 0

Java compiling errors


Question

the problem occurs in compiling when it reaches a line using the maths system of java.

choice = (Math.round(Math.random()*100));

this line keeps coming up with the error in compiling saying "possible loss of precision"

ive looked around on the internet for a solution to this, but the most i could find was people saying to change the variable to a floating interger or double. i have tried both of these to no avail as it will get past that line, then freeze on the next line where the variable choice is used again.

any clues?

if more of the code is needed to help just ask.

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

for some reason its coming up as an error and not allowing a complete compile of the java structure.

as for type casts, where do i type that and what is it? im still mostly learning java here. but im already developing a pet hate to it.

Link to comment
Share on other sites

  • 0

well ive done that. but to get it to not give an error on compiling i have to do:

float choice = (float)(Math.round(Math.random()*100.0));

but then it gets stuck on the next line with the same error again.

this is the whole segement from which it is from

       if (whosturn==1){
            float choice = (float)(Math.round(Math.random()*100.0));
            if (computershot[choice]==0){
                computershot[choice]=1;
                computerx[computernumber]=computerlocationx[choice];
                computery[computernumber]=computerlocationy[choice];
                computernumber++;
                whosturn=0;
                repaint();
            }
            else if (computershot[choice]==1){
                choice = (Math.round(Math.random()*100));
                repaint();
            }
        }
        for (int i=1;i<=100;i++){
            g.drawImage(hitpicture,computerx[i],computery[i],this);
        }

Link to comment
Share on other sites

  • 0

Hi,

it looks to me that the loss os precision is due to the variable choice is being used to reference an entry in the array computershot. You can only reference arrays with an integer type, whereas at the moment you are using a floating point type. If you make the following change it should compile past this part....

Old:

float choice = (float)(Math.round(Math.random()*100.0));

New:

int choice = (int)(Math.round(Math.random()*100.0));

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.