Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: One fast question

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default One fast question

    Would you please tell me why my output is always 0.0 ? i want my program to print the sum of the values that are less than the average of all of the values generated .
    import java.util.*;
     
    public class Test {
    	public static void main(String[] args) {
    		double[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    		double result = sumLessThanAverage(numbers);
    		System.out.println(result);
    	}
     
    	public static double sumLessThanAverage(double[] a) {
    		int sum = 0;
    		int less = 0;
    		for (int i = 0; i < a.length-1; i++) {
     
    			sum += a[i];
    			double crazy = sum / a.length;
    			if (i < crazy)
    				less += i;
     
    		}
     
    		return (double) less;
     
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,309
    My Mood
    Mellow
    Thanks
    252
    Thanked 780 Times in 765 Posts

    Default Re: One fast question

    Please give your threads better titles. Fast questions are never fast, easy never easy, etc. If they were, why would you need to ask them?

    Class names begin with capital letters in Java. It's important.

    Fast answer: The reason your output is always zero is because of integer math.

    If you want a longer answer, let us know (but don't start a new thread called "One Long Answer.")

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One fast question

    Yes I do like a longer answer , and sorry for posting my thread titled " Easy " will be making sure next time to write my title better.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,309
    My Mood
    Mellow
    Thanks
    252
    Thanked 780 Times in 765 Posts

    Default Re: One fast question

    An integer divided by an integer will result in an integer not a decimal. What's 5 / 3 when 5 and 3 are both integers? Try it and see. What's 3 / 5? You've already tried that. What was the answer? To achieve a double result, one of the numbers must be something other than an integer. Try the same calculations above using 5.0 / 3, 5 / 3.0, and 3.0 / 5.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One fast question

    Yes i am aware of these facts that integer divided by an integer will result an integer but if a double divided by an integer will result a double , however i treid setting all of my int to double and insted of
    {1,2,3,4,5,6,7,8,9}
    it is now
    { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }
    But i am still struggling with the same problem.
    Please be patient with me Greg i am sorry for not solving the problem but as i said i'm still a beginner thank you.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,309
    My Mood
    Mellow
    Thanks
    252
    Thanked 780 Times in 765 Posts

    Default Re: One fast question

    Focus on this:

    double crazy = sum / a.length;

    In fact, describe what the method sumLessThanAverage() is supposed to do. The method returns 'less'. When will less be anything other than 0?

Similar Threads

  1. Fast Help please
    By Extremist252 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 25th, 2013, 06:01 PM
  2. need HELP FAST PLEASE HELP ME
    By britben95 in forum Member Introductions
    Replies: 1
    Last Post: November 14th, 2011, 08:05 PM
  3. need help fast
    By coke32 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 1st, 2011, 12:04 AM
  4. need help fast!!
    By nwollis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 06:12 PM
  5. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 10:00 AM