Results 1 to 7 of 7
Thread: Question about sum+=
 09282013, 08:28 PM #1Curious Java Student
 Join Date
 Sep 2013
 Location
 Somewhere in the vast valley of nowhere special
 Posts
 8
 Rep Power
 0
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."); } }
What function should I use instead of sum+ to fix this tiny problem?
 09282013, 10:06 PM #2Member
 Join Date
 Sep 2013
 Posts
 10
 Rep Power
 0
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 +=
 09282013, 10:18 PM #3Senior Member
 Join Date
 Aug 2013
 Location
 Sweden
 Posts
 161
 Rep Power
 2
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: " + (x1) + "\nBiggest number: " + biggest + "\nSmallest number: " + smallest); } }
 09282013, 11:16 PM #4Member
 Join Date
 Sep 2013
 Posts
 10
 Rep Power
 0
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.
 09292013, 02:16 AM #5Curious Java Student
 Join Date
 Sep 2013
 Location
 Somewhere in the vast valley of nowhere special
 Posts
 8
 Rep Power
 0
 09292013, 01:28 PM #6Member
 Join Date
 Sep 2013
 Posts
 10
 Rep Power
 0
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
 09292013, 07:01 PM #7Curious Java Student
 Join Date
 Sep 2013
 Location
 Somewhere in the vast valley of nowhere special
 Posts
 8
 Rep Power
 0
Similar Threads

Forum question on why discussion threads are locked though question is unanswered
By pseeburger in forum Suggestions & FeedbackReplies: 2Last Post: 05252012, 05:00 PM 
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12132011, 05:01 PM 
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11182011, 02:14 AM 
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01142011, 01:05 AM 
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12192010, 09:49 AM
Bookmarks