|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Sep 2007
Messaggi: 478
|
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?? |
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
posti il pezzo di codice in questione?
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Sep 2007
Messaggi: 478
|
float[]dif=new float[a];
float[]cen=new float[b]; float[]att=new float[c]; float totale=dif[i]+cen[i]+att[i]; |
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Sep 2007
Messaggi: 478
|
a,b,c e i sono tutti float
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Feb 2006
Messaggi: 1304
|
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 |
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Sep 2007
Messaggi: 478
|
ok adesso sono rimasti 3 errori, devo uguagliare a,b e c a 3 variabili float...
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Sep 2007
Messaggi: 478
|
ok problema della variabili risolto, adesso eseguendo il programma si interrompe dopo il primo for, quando invece ne dovrebbe eseguire 3!! qualcuno può aiutarmi?
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Mar 2008
Messaggi: 401
|
ehm... non credi che forse dovresti darci il codice?
P.S.: Ricorda i tag code |
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Sep 2007
Messaggi: 478
|
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?? |
|
|
|
|
#10 | ||
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
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;
}
Quote:
Codice:
// 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);
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
||
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:54.


















