I have tried to do a loop, but it either outputs the same number, e.g. hasn't added the previous input. Or it now comes out with NaN. (The average output is sent to jTextField4).
Please help!!
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
float total = 0;
float i;
float num3 = 0;
float averageMpg;
num3 = Float.parseFloat(jTextField3.getText());
for (i=0; i<=100; i++) {
total = total + num3;
}
averageMpg = total/(i-1); // i is 4 after the loop has finished incrementing
jTextField4.setText(String.valueOf(averageMpg));
I also have this code that did the same thing:
float num3;
float total = 0;
int count = 0;
float averageMpg = 0;
num3 = Float.parseFloat(jTextField3.getText());
if (total < 0){
total+=num3;
count++;
}
averageMpg = total/count;
jTextField4.setText(String.valueOf(averageMpg));
Edited by macosxnerd101: Welcome to DIC!
This post has been edited by macosxnerd101: 18 May 2010 - 05:53 PM

New Topic/Question
Reply




MultiQuote









|