Results 1 to 8 of 8
Thread: Mean computation
- 02-21-2011, 06:01 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
Mean computation
import javax.swing.JOptionPane;
public class ComputeMean {
/**
* @param args
* @return
*/
public static double main(String[] args) {
String mark = JOptionPane.showInputDialog("Enter Marks to calculate Mean:");
int marks = Integer.parseInt(mark);
double sum = 0;
int i = 0;
double average = 0;
while (marks != -1) {
sum = sum + marks;
average = sum / i;
i++;
return average;
}
if (marks == -1){
JOptionPane.showMessageDialog(null, "The average mark is "+average+"");
}
return average;
}
}
I made this code to compute the average of the marks that are input by the user. for some reason It wont run :/
could someone help me out please
- 02-21-2011, 06:05 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try using a scanner and an array, prompt for marks and fill the array with the inputs. From there you can use the array to very easily compute average with a loop.
- 02-21-2011, 06:11 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
I haven't learned how to use arrays. Is it possible to do this without arrays?
- 02-21-2011, 06:17 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
it is, Do you know how to use a scanner? Your code is trying to do way too much. Simply get the mark, store it, convert it to an int with Integer.parseInt(). Then do sum += mark. Continue this process until you have collected all the marks you want. Once done, sum will be calculated, and you can easily compute average from there.
You may need a loop to prompt for the mark and add it to the sum.
- 02-21-2011, 06:18 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
I do not know how to use scanner :/
- 02-21-2011, 06:25 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
Is it possbile to like get the input for "marks", get it through the loop to compute sum and average and then clear off mark for the next input?
- 02-21-2011, 08:51 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes you can reassign it. Try looking up the scanner api, it's easy to use. EAch step through the loop should get some input and add it to the sum.getting input reassigns mark
- 02-22-2011, 12:00 AM #8
Similar Threads
-
Basic Java Computation Help Ap Computer Science
By Sean_J in forum New To JavaReplies: 6Last Post: 02-10-2010, 01:10 PM -
automatic cell computation in a JTable like an excel spread sheet
By bigj in forum New To JavaReplies: 2Last Post: 02-01-2010, 03:58 PM -
Computation Complexities for a stack, queue, and map?
By daletron3030 in forum New To JavaReplies: 0Last Post: 03-18-2009, 06:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks