Results 1 to 8 of 8
  1. #1
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default 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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    I haven't learned how to use arrays. Is it possible to do this without arrays?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    I do not know how to use scanner :/

  6. #6
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    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?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    The problem in the code you posted is that you ask for user input ONCE outside the loop. Simply move that inside the loop.

Similar Threads

  1. Basic Java Computation Help Ap Computer Science
    By Sean_J in forum New To Java
    Replies: 6
    Last Post: 02-10-2010, 01:10 PM
  2. Replies: 2
    Last Post: 02-01-2010, 03:58 PM
  3. Computation Complexities for a stack, queue, and map?
    By daletron3030 in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •