Results 1 to 7 of 7
  1. #1
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Question about sum+=

    I have a program, that is supposed to calculate the sum of the numbers, retrieve the smallest of the set, and how many numbers were entered.

    But, the sum+= function I have is not properly calculating when negative numbers are entered
    Java Code:
    {
    public static void main(String [] args)
    {
    int numberOfInt;
    int numberIn;
    int small;
    
    int sum = 0;
    Scanner input = new Scanner(System.in);
    
    System.out.print("Please, enter the numbers you would like in the set: ");
    numberOfInt = input.nextInt();
    
    if(numberOfInt > 0)
    {
    	System.out.print("Please enter your first number: ");
    	numberIn = input.nextInt();
    	small = numberIn;
    	for(int j = 2 ; j <= numberOfInt ; j++)
    	{
    		sum += numberIn;
    		System.out.print("Please enter your next number: ");
    		numberIn = input.nextInt();
    		if(numberIn < small)
    		small = numberIn;
    	}
    	System.out.println("The sum of set is: " + sum);
    	System.out.println("The smallest number in the set is: " + small);
    	System.out.println("The number of numbers entered was: " + numberOfInt);
    }
    else
    	System.out.println("The set empty, therefore no sum, smallest number or count exist.");
    	
    }
    }
    For instance with a data set of -1, -2, and 8. The sum printed should be 5, and my program prints -3.
    What function should I use instead of sum+ to fix this tiny problem?

  2. #2
    Ubiquitous is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Question about sum+=

    Take a look at the logic in your loop. Do it by hand it's a simple mistake and it is not an issue with +=

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Question about sum+=

    Wouldn't it be easier if you just had a loop that continues until the user chooses to quit? Somewhat like this:

    Java Code:
    import java.util.*;
    
    public class Bajs{
    	public static void main(String[] args){
    		Scanner hej = new Scanner(System.in);
    		int x = 1, number, biggest = 0, smallest = 0, sum = 0;
    		System.out.print("Enter your first number: ");
    		while(hej.hasNextInt()){
    			System.out.print("Enter number " + (x+1) + ": ");
    			number = hej.nextInt();
    			if(x == 1){
    				biggest = number;
    				smallest = number;
    			}else{
    				if(number > biggest)
    					biggest = number;
    				else if(number < smallest)
    					smallest = number;
    			}
    			sum += number;
    			x++;
    		}
    		System.out.print("Sum: " + sum + "\nThe number of numbers entered: " + (x-1) + "\nBiggest number: " + biggest + "\nSmallest number: " + smallest);
    	}
    }

  4. #4
    Ubiquitous is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Question about sum+=

    @ Zelaine I don't believe it is an issue of making the code easier or simpler. He/she is having an issue with a number not being added and he/she needs to understand what is happening. In providing your version of the code you eliminate the opportunity of them being able find out the issue they are having. Any issue no matter how simple it may be to you might not be so for them until they have experience in looking for it themselves.

  5. #5
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Re: Question about sum+=

    Quote Originally Posted by Ubiquitous View Post
    Take a look at the logic in your loop. Do it by hand it's a simple mistake and it is not an issue with +=
    I think I have found it. Does += need to go after the 22nd line?

  6. #6
    Ubiquitous is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Question about sum+=

    Going based of your posted code line 22 contains S.o.p("Please enter your next number: "); If you were to put it after this line the issue would still remain but you are close. The issue is you add at the beginning of the loop which is not a good idea considering you get the value later on in the loop.

    To clarify think of it as this:
    Java Code:
    sum = 0;
    counter = 0;
    number = userInput; // Number = 5;
    while (counter < 2) // Is 0 < 2 ? Yes
    sum += number; // sum = 5
    number = userInput; // Number = 21
    counter++; // counter = 1
    whlie (counter < 2) // Is 1 < 2 ? Yes
    sum += number; // sum = 26;
    number = userInput; // -15;
    counter++; // counter = 2;
    while (counter < 2) // Is 2 < 2 ? No
    Don't run code in loop
    S.o.p ("Total sum is: " + sum) // sum = 26
    Hope this makes clarifies as to why the addition at the start of your loop is a bad thing.

  7. #7
    Monteezy is offline Curious Java Student
    Join Date
    Sep 2013
    Location
    Somewhere in the vast valley of nowhere special
    Posts
    8
    Rep Power
    0

    Default Re: Question about sum+=

    Not sure if I correct the problem the right way, but adding sum += after line 18 seems to have corrected my issue. Thanks for all the help!

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 05:00 PM
  2. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 05:01 PM
  3. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 02:14 AM
  4. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 01:05 AM
  5. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM

Tags for this Thread

Posting Permissions

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