Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By sunde887

Thread: more efficient way to make this??? [LOOK]

  1. #1
    Green's Avatar
    Green is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default more efficient way to make this??? [LOOK]

    Java Code:
    import java.util.Scanner;
    
    public class first {
    	public static void main (String [] args){
    		Scanner reader = new Scanner(System.in);
    
    		int total_age;
    		int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10, age11, age12, age13, age14, age15, age16, age17, age18, age19;
    		int mean_age;
    
    		System.out.print("Enter the age of the students ");
    		age1 = reader.nextInt();
    		age2 = reader.nextInt();
    		age3 = reader.nextInt();
    		age4 = reader.nextInt();
    		age5 = reader.nextInt();
    		age6 = reader.nextInt();
    		age7 = reader.nextInt();
    		age8 = reader.nextInt();
    		age9 = reader.nextInt();
    		age10 = reader.nextInt();
    		age11 = reader.nextInt();
    		age12 = reader.nextInt();
    		age13 = reader.nextInt();
    		age14 = reader.nextInt();
    		age15 = reader.nextInt();
    		age16 = reader.nextInt();
    		age17 = reader.nextInt();
    		age18 = reader.nextInt();
    		age19 = reader.nextInt();
    
    		total_age = age1 + age2 + age3 + age4 + age5 + age6 + age6 + age8 + age9 + age10 + age11 + age12 + age13 + age14 + age15 + age16 + age17 + age18 + age19;
    		mean_age = total_age / 19;
    		System.out.println(mean_age);
    
    
    	}
    }
    can anyone think of a smarter way to write this? its asking for 19 students ages and then finding the mean of them...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: more efficient way to make this??? [LOOK]

    A loop with the input values summed in the loop. If there is no need for the individual ages after adding them to the total, don't save them in variables.

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

    Default Re: more efficient way to make this??? [LOOK]

    I looked but wasn't impressed.

  4. #4
    Green's Avatar
    Green is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: more efficient way to make this??? [LOOK]

    Quote Originally Posted by Junky View Post
    I looked but wasn't impressed.
    that would be why i am asking for help, because i am a beginner and posting in the New To Java section, you retard.

    Quote Originally Posted by Norm View Post
    A loop with the input values summed in the loop. If there is no need for the individual ages after adding them to the total, don't save them in variables.
    an example would look like?
    Last edited by Green; 09-09-2011 at 01:20 AM.

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

    Default Re: more efficient way to make this??? [LOOK]

    My previous post was in response to you placing LOOK in the title. This is the same as those that place URGENT in the title. You only do it for one reason: draw attention to yourself in the hop of gaining preferential treatment. Well you are no more important than anyone else asking for help. People will offer help if and when they like. I have now made 2 posts in this thread and neither have offered you any help. So pimping your thread has actually reduced your chances of getting help not increased it. Many others feel the same way as I do.

    Feel free to get all offended, throw a hissy fit and hurl abuse my way. That just further decreases your chances of getting help.

  6. #6
    Green's Avatar
    Green is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: more efficient way to make this??? [LOOK]

    pfft. i will - rep you and you must think that i care if you wont help me. i am sure someone will offer advice sooner or later.
    /trollolloooooolll

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

    Default Re: more efficient way to make this??? [LOOK]

    Yes I'm sure someone will because you are so fabulous that you can abide by your own rules. Such a rebel.

  8. #8
    Green's Avatar
    Green is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: more efficient way to make this??? [LOOK]

    Quote Originally Posted by Junky View Post
    Yes I'm sure someone will because you are so fabulous that you can abide by your own rules. Such a rebel.
    gtfo my thread, spammer. 2.4k posts since January LMAO

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

    Default Re: more efficient way to make this??? [LOOK]

    I wish we had a troll face smiley. @op: Don't get offended, just take his advice and move on. Seeing [LOOK], URGENT, SUPER DUPER IMPORTANT, won't get your post help any quicker(and it may hurt you).
    Last edited by sunde887; 09-09-2011 at 04:59 AM.
    Fubarable likes this.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: more efficient way to make this??? [LOOK]

    set total and count to 0
    loop until all input read
    read input
    add input to total
    add 1 to count
    end loop
    average = total / count

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

    Default Re: more efficient way to make this??? [LOOK]

    You really aren't very bright are you? Did you bother to read where I said that throwing a hissy fit and getting abusive further decreases your chances of getting help? Then right on cue you do that very thing. Why don't you complete the set and accuse me of having no life and living in my parents basement because I've never heard that one before.

Similar Threads

  1. Tell me efficient way
    By lkalaivanan in forum New To Java
    Replies: 3
    Last Post: 08-11-2011, 12:36 PM
  2. Efficient image loading
    By aburnett397 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2011, 03:44 PM
  3. is there a more efficient way?
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 06:32 PM
  4. Replies: 1
    Last Post: 03-22-2010, 09:21 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
  •