-
March 25th, 2012, 12:44 PM
#1
Average grade program
i need help with my array ad parameters i don't know how to fix this program.
This is a program that allows a user to enter class grades into an array of floats. The app will prompt for the total number of grades to be entered, then call a method to average the grades, and display the grades and the resulting average.
This is my code
import javax.swing.JOptionPane;
public class Average {
public static void main(String[]args)
{
String input,amount;
double data[], gradetotal1;
double sum=0,average;
System.out.println("\tAverage Program");
amount=JOptionPane.showInputDialog("Input Number of grades to enter");
gradetotal1=Double.parseDouble(amount);
data[]=gradetotal1;
for (int i=0;i<data.length;i++){
input=JOptionPane.showInputDialog("Enter grades");
data[i]=Double.parseDouble(input);
}
for(int i=0;i<data.length;i++){
sum+=data[i];
average=sum/data.length;
JOptionPane.showMessageDialog(null, "The total is:"+sum+"The Average is:"+average, JOptionPane.INFORMATION_MESSAGE);
}
}
}
-
March 26th, 2012, 05:07 AM
#2
Re: Average grade program
Try this, now finding out what I have changed and why is your job
Code:
import javax.swing.JOptionPane;
public class Average{
public static void main(String[] args) {
String input, amount;
double data[];
int gradetotal1;
double sum = 0, average = 0;
System.out.println("\tAverage Program");
amount = JOptionPane.showInputDialog("Input Number of grades to enter");
gradetotal1 = Integer.parseInt(amount);
data = new double[gradetotal1];
for (int i = 0; i < data.length; i++) {
input = JOptionPane.showInputDialog("Enter grades");
data[i] = Double.parseDouble(input);
}
for (int i = 0; i < data.length; i++) {
sum += data[i];
average = sum / data.length;
}
JOptionPane.showMessageDialog(null, "The total is: " + sum
+ " The Average is: " + average, "Result",
JOptionPane.INFORMATION_MESSAGE);
}
}
Last edited by javinpaul; March 26th, 2012 at 05:15 AM.
-
March 27th, 2012, 06:39 AM
#3
Re: Average grade program
data[] = new double(gradetotal1);
-
March 27th, 2012, 07:12 AM
#4
Re: Average grade program
Code:
data[] = new double(gradetotal1);
What is that supposed to do?
-
March 27th, 2012, 08:49 AM
#5
Re: Average grade program
Just corrected a mistake he said data[] = gradetotal1; which is a mistake.
-
March 27th, 2012, 10:50 AM
#6
Re: Average grade program
Originally Posted by cens
data[] = new double(gradetotal1);
Originally Posted by cens
Just corrected a mistake he said data[] = gradetotal1; which is a mistake.
The correct code (which is in javinpaul's last post) is
Code:
double data[];
...
data = new double[gradetotal1];
You don't need the [] in the assignment, just in the declaration.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|