• 09-28-2013, 07:28 PM
Monteezy
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
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?
• 09-28-2013, 09:06 PM
Ubiquitous
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 +=
• 09-28-2013, 09:18 PM
Zelaine
Wouldn't it be easier if you just had a loop that continues until the user chooses to quit? Somewhat like this:

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);         } }```
• 09-28-2013, 10:16 PM
Ubiquitous
@ 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.
• 09-29-2013, 01:16 AM
Monteezy
Quote:

Originally Posted by Ubiquitous
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?
• 09-29-2013, 12:28 PM
Ubiquitous
```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```