View Full Version : Problema con array di float
Utilizzo Java
il compilatore mi segnala errore in presenza di array di float. il messaggio che compare è il seguente:
possible loss of precision
found:float
required:int
ma io devo usare i float! come fare??:confused: :confused: :confused:
posti il pezzo di codice in questione?
float[]dif=new float[a];
float[]cen=new float[b];
float[]att=new float[c];
float totale=dif[i]+cen[i]+att[i];
a,b,c e i sono tutti float
Il codice che hai postato va bene... Il problema semplicemente è il tipo di i.
Come indici vanno bene solo i tipi interi(unsigned)... basta che i lo dichiari come int od unsigned int e va bene :D
ok adesso sono rimasti 3 errori, devo uguagliare a,b e c a 3 variabili float...
ok problema della variabili risolto, adesso eseguendo il programma si interrompe dopo il primo for, quando invece ne dovrebbe eseguire 3!! qualcuno può aiutarmi?
agente mm8
14-01-2009, 19:00
ehm... non credi che forse dovresti darci il codice? :)
P.S.: Ricorda i tag code ;)
il problema dei for l'ho risolto, ora si eseguono normalmente ma nel totale mi da errore ed esce dal programma. nel totale devo prendere tutti i valori degli array ecco il codice:
int celle=Integer.parseInt(JOptionPane.showInputDialog("Quante celle?");
float[]cel=new float[celle];
for (a=0;a<celle;a++)
{
float valcel=Float.parseFloat(JOptionPane.showInputDialog("Inserisci valore celle "+a));
cel[a]=valcel;
}
int celle2=Integer.parseInt(JOptionPane.showInputDialog("Quante celle2?"));
float[]cel2=new float[celle2];
for (b=0;b<celle2;b++)
{
float valcel2=Float.parseFloat(JOptionPane.showInputDialog("Inserisci valore celle2 "+b));
cel2[b]=valcel2;
}
int celle3=Integer.parseInt(JOptionPane.showInputDialog("Quante celle3?"));
float[]cel3=new float[celle3];
for (c=0;c<celle3;c++)
{
float valcel3=Float.parseFloat(JOptionPane.showInputDialog("Inserisci valore celle3 "+c));
cel3[c]=valcel3;
}
float totale?????(qui cosa devo mettere x sommare tutti i valori dei 3 array??
banryu79
16-01-2009, 11:12
il problema dei for l'ho risolto, ora si eseguono normalmente ma nel totale mi da errore ed esce dal programma. nel totale devo prendere tutti i valori degli array ecco il codice:
int celle = Integer.parseInt(JOptionPane.showInputDialog("Quante celle?");
float[]cel = new float[celle];
for (a=0; a<celle; a++)
{
float valcel=Float.parseFloat(JOptionPane.showInputDialog("Inserisci valore celle "+a));
cel[a]=valcel;
}
int celle2 = Integer.parseInt(JOptionPane.showInputDialog("Quante celle2?"));
float[]cel2 = new float[celle2];
for (b=0; b<celle2; b++)
{
float valcel2=Float.parseFloat(JOptionPane.showInputDialog("Inserisci valore celle2 "+b));
cel2[b]=valcel2;
}
int celle3 = Integer.parseInt(JOptionPane.showInputDialog("Quante celle3?"));
float[]cel3 = new float[celle3];
for (c=0; c<celle3; c++)
{
float valcel3=Float.parseFloat(JOptionPane.showInputDialog("Inserisci valore celle3 "+c));
cel3[c]=valcel3;
}
Dovresti postare anche il codice con cui calcoli il totale.
float totale?????(qui cosa devo mettere x sommare tutti i valori dei 3 array??
Comunque per fare il totale basta che sommi tutti i valori di un array per avere il totale di quell'array, e alla fine sommi i tre totali.
// restituisce la somma di tutti i valori contenuti nel float array 'arr'
private float totArray(float[] arr)
{
float total = 0;
for (int i = 0; i < arr.lenght; i++)
total += arr[i];
}
// codice che usa il metodo qui sopra:
float totale = totArray(cel) + totArray(cel2) + totArray(cel3);
http://www.hwupgrade.it/forum/showthread.php?t=1649196
vBulletin® v3.6.4, Copyright ©2000-2026, Jelsoft Enterprises Ltd.