Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Getting the sum of the numbers in an array. Help Please!

    Where did you learn to count? I can see four of those method calls in your latest code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Getting the sum of the numbers in an array. Help Please!

    thats embarrassing :/ haha well i took out one of those and it takes 3 inputs now, but only adds the last 2.

  3. #23
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Getting the sum of the numbers in an array. Help Please!

    I got it to work. Here's the code that made it work.
    Java Code:
    import java.util.Scanner;
    public class Array2 
    {
    	public static void main(String[]args)
    	{
    		
    		Scanner sj = new Scanner(System.in);
    		System.out.println("Please input your three numbers");
    		
    		
    		int[] numbers1 = new int[3];
    		numbers1[0] = sj.nextInt();
    		numbers1[1] = sj.nextInt();
    		numbers1[2] = sj.nextInt();
    		
    		
    		
    		int sum = 0;
    		
    		for(int i = 0; i < numbers1.length; i++)
    		{
    			sum += numbers1[i];
    			
    		}
    		
    		System.out.println("Here's the sum " + sum);
    		
    		
    		
    	}
    
    }

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Getting the sum of the numbers in an array. Help Please!

    Quote Originally Posted by mike_ View Post
    I got it to work. Here's the code that made it work....
    Java Code:
    import java.util.Scanner;
    public class Array2 
    {
    	public static void main(String[]args)
    	{
    		
    		Scanner sj = new Scanner(System.in);
    		System.out.println("Please input your three numbers");
    		
    		
    		int[] numbers1 = new int[3];
    		numbers1[0] = sj.nextInt();
    		numbers1[1] = sj.nextInt();
    		numbers1[2] = sj.nextInt();
    		
    		
    		
    		int sum = 0;
    		
    		for(int i = 0; i < numbers1.length; i++)
    		{
    			sum += numbers1[i];
    			
    		}
    		
    		System.out.println("Here's the sum " + sum);
    		
    		
    		
    	}
    
    }
    Good deal, but I'd still get the user input inside of a for loop. This part here:

    Java Code:
            int[] numbers1 = new int[3];
            numbers1[0] = sj.nextInt();
            numbers1[1] = sj.nextInt();
            numbers1[2] = sj.nextInt();

  5. #25
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Getting the sum of the numbers in an array. Help Please!

    Quote Originally Posted by Fubarable View Post
    Good deal, but I'd still get the user input inside of a for loop. This part here:

    Java Code:
            int[] numbers1 = new int[3];
            numbers1[0] = sj.nextInt();
            numbers1[1] = sj.nextInt();
            numbers1[2] = sj.nextInt();
    How would that work out?

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Page 2 of 2 FirstFirst 12

Similar Threads

  1. adding numbers in an array together
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 07:23 PM
  2. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 09:33 PM
  3. Swapping numbers in an array
    By Awt582 in forum New To Java
    Replies: 3
    Last Post: 11-01-2010, 12:03 AM
  4. How do I place these numbers into an array
    By mainy in forum New To Java
    Replies: 2
    Last Post: 07-19-2009, 06:57 PM
  5. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 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
  •