Results 1 to 5 of 5
  1. #1
    anonymous18 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Why is the answer not coming out

    public class Lab8_Ex1
    {
    public static void main(String [] args)
    {
    int sum = 0;
    int n = 10;
    compute_sum(n);
    System.out.println(“The sum is “ + sum);
    System.exit(0);
    }
    public static void compute_sum(int x)
    {
    int sum = 0;
    int num = 1;
    while (num <=x)
    {
    sum += num;
    num++;
    }
    }
    }

    It's supposed to sum up the no. 1-10

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you need to return values from a method call for them to be used. You are printing a number that is initialized to zero and is never modified.

  3. #3
    anonymous18 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Ok, so I'm guessing I would have to put a return; in the program but I'm still not really understanding should I change oner of the initialized zeros, and I put a return in and still got the same thing, could it be in the wrong place

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Return the value...

    What emceenugget is telling you to do is return the result of the sum you need from the compute_sum method... something like
    Java Code:
    return (sum);
    In the main method you have to pick up the sum with
    Java Code:
    sum = compute_sum(n);
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    anonymous18 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2008, 01:29 AM
  2. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 06:36 PM
  3. How can i know from which jsp request is coming?
    By vishnujava in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2008, 01:13 PM
  4. What is the answer yo my problem with this code
    By masaka in forum New To Java
    Replies: 4
    Last Post: 03-26-2008, 06:33 AM
  5. MySQL Training Class coming to Columbus, OH
    By pegitha in forum Reviews / Advertising
    Replies: 0
    Last Post: 05-07-2007, 05:07 PM

Posting Permissions

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