Correction 'un programme

Bonjour,je peux pas corriger ce programme

int p;
int pro;
System.out.println("Donner la probabilite");

for(i=0;i<n-1;i++)
{
p=Math.random()%10000;
if (p<pro);
{
t2[i]=t[i];
}
else
{
if(t[i]==0)
t2[i]=1;
else
t2[i]=0;
}

}

---------- Compiler ---------- c2.java:164: 'else' without 'if'
else
^ c2.java:159: possible loss of precision
found : double
required: int
p=Math.random()%10000;
^
2 errors

Output completed (0 sec consumed) - Normal Termination
Configuration: Windows Vista
Internet Explorer 7.0

2 réponses

  1. Math.random() retourne un nombre compris entre ]0,1[ donc float ou double.
    0
    1. p doit etre un double et faut enlever le ; apres le if
      double p; 
      int pro; 
      System.out.println("Donner la probabilite"); 
      
      for(i=0;i<n-1;i++) 
      { 
      p=Math.random()%10000; 
      if (p<pro)
      { 
      t2[i]=t[i]; 
      } 
      else 
      { 
      if(t[i]==0) 
      t2[i]=1; 
      else 
      t2[i]=0; 
      } 
      
      }
      0
      1. merci pour votre aide, mes il me reste une autre erreure
        ---------- Compiler ---------- c2.java:160: variable pro might not have been initialized
        if (p<pro)
        ^
        1 error

        Output completed (0 sec consumed) - Normal Termination
        0
      2. @zeinil faut que tu initialise ta variable pro
        0
      3. @mypeMerci encore une fois, ça marche maintenant.
        0