Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with exercise.

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Help with exercise.

    My code is working. It currently asks the user to enter 10 numbers via a ForLoop. What I cant figure out how to do, is how I would go about adding the number that the user enters first to the second and adding that to the third etc? Any ideas?

    import sun.jvm.hotspot.gc_implementation.parallelScavenge.PSYoungGen;
     
    import java.util.Scanner;
     
    /**
     * Created with IntelliJ IDEA.
     * User: polmcguigan
     * Date: 07/02/2013
     * Time: 12:55
     * UPDATE COMMENT ABOUT PROGRAM HERE
     */
    public class TotalNumbers
    {
       public static void main(String[] args)
       {
          String name;
          final int REPEATUNTIL=10;
          int number,totalnumber, numberentered;
     
          Scanner keyboard = new Scanner(System.in);
     
          System.out.println("What is your name?");
          name=keyboard.nextLine();
     
          System.out.println("Please enter 10 whole numbers.");
          for (int count = 1; count <= REPEATUNTIL; number++)
          {
             System.out.println("Please enter a number ");
             number=keyboard.nextInt();
     
          }
     
     
     
       }//main
    }//class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with exercise.

    how I would go about adding the number
    Define a separate variable to hold the sum of the numbers.
    Add each number that is read to that variable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with exercise.

    You already have a totalnumber variable. Just initialize that to 0 and then each loop add whatever the person entered to that variable.

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Help with exercise.

    Quote Originally Posted by Parranoia View Post
    You already have a totalnumber variable. Just initialize that to 0 and then each loop add whatever the person entered to that variable.
    Would this require another variable? To add what the person entered would it be:
    totalnumber=number+..? Can you tell me what it would be?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with exercise.

    There is a shortcut operator (+=) that you can use to add one variable to another:
    theTotal += aDetail;
    instead of
    theTotal = theTotal + aDetail;
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Help with exercise.

    Thank you Norm.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Help with exercise.

    Quote Originally Posted by javapol View Post
    Thank you Norm.
    Where can I find more of these shortcut operators?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with exercise.

    Look at the tutorials:
    Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    also ask google
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. exercise solution ???
    By somebodyonearth in forum Java Theory & Questions
    Replies: 1
    Last Post: March 11th, 2012, 12:52 PM
  2. Need help with programming exercise.
    By X0BeachBabe0x in forum Loops & Control Statements
    Replies: 2
    Last Post: December 29th, 2011, 10:08 PM
  3. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  4. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM
  5. Exercise problems
    By Virgildonatifan in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 04:12 AM