Exercise 36.2: Printing The Sum Trouble!

177 views
Skip to first unread message

seanmaa...@gmail.com

unread,
Jan 1, 2016, 1:43:41 PM1/1/16
to mooc.fi
Hi,

I'm having problems with week exercise 36.2.

I'm currently working on 36.2. I can't figure out how to print the sum of all the user input! Only one of the numbers is output. I've tried looking around online for similar problems with solutions, but they use concepts I haven't learned yet (do and for). How does one go about doing this with a while-loop? I have a feeling the sum+=variable trick isn't the right one. I'm guessing that the loop is broken with break before more numbers can be admitted. How do I prevent this breaking from happening if that is the case? 


For now, I'll move on to some other exercises

Best regards,

Sean

Supported languages are English and Finnish

Please use our build in TMC Pastebin when asking questions related to your code  
 
TMC Plugin has built-in code sharing feature. 
In NetBeans, select TMC -> Send code to TMC pastebin. Copy the link you receive and paste it to your message.  

You may use our web interface to share code snippets: http://paste.mooc.fi

Aleksandr Oleshko

unread,
Jan 1, 2016, 2:29:46 PM1/1/16
to mooc.fi, seanmaa...@gmail.com
Hi there Sean.

Using plain words to describe the sequence of actions (algorithm), you would need to do the following in each round of the loop:
  1. ask the user for the input
  2. check if the input equals to the "exit" code
  3.  if it does, then print the lines given from the template with the exact wording, add the variable with the value of the summed up values from each round (see next step)
  4.  if it doesnt, oh well, life is life, add the number to a variable, which keeps the summed up values from each round. 
    sumOfAll += userEntered; // dont forget to declare the sumOfAll before the loop: int sumOfAll = 0;
  5. end of loop round, returning to step 1

Hope this helps :). Good luck!

Alex

seanmaa...@gmail.com

unread,
Jan 2, 2016, 6:33:07 PM1/2/16
to mooc.fi, seanmaa...@gmail.com
Hey, Alex! 

I was about to send another message asking for further help, but now I've got it! I moved some lines into the loop and added another line of code to make the loop end better. The loop needed to be a little more complex than I expected. Thank you.

I hope your new year was well,
All the best, Sean
Reply all
Reply to author
Forward
0 new messages