Basic java while loop help please.

Discussion in 'Programming General' started by Lerker, Feb 6, 2015.

Basic java while loop help please.
  1. Unread #1 - Feb 6, 2015 at 7:25 AM
  2. Lerker
    Joined:
    Apr 1, 2014
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0

    Lerker Active Member
    Banned

    Basic java while loop help please.

    Sorted it, thankyou
     
  3. Unread #2 - Feb 6, 2015 at 7:49 AM
  4. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Basic java while loop help please.

    Looks like you are starting the count at 0 and incrementing upwards. How can you expect the loop to ever get to a negative number (-9999) if you are incrementing? I think what you want is:

    sum-=sc.nextInt();

    Unless you are inputting negative numbers..
     
  5. Unread #3 - Feb 6, 2015 at 8:07 AM
  6. Lerker
    Joined:
    Apr 1, 2014
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0

    Lerker Active Member
    Banned

    Basic java while loop help please.

    I just want to add a list of numbers, then the program to stop once I input -9999 and print out the sum.

    Example:

    1
    5
    6
    8
    9
    -9999.

    Sum = 29.
     
  7. Unread #4 - Feb 6, 2015 at 8:56 AM
  8. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Basic java while loop help please.

    Code:
    Scanner sc = new Scanner(System.in);
    int i=0;
    double sum = 0.0;
    
    System.out.println(" Enter a list of whole numbers, then type -9999 once your list is complete.");
    i = sc.nextInt();
    sum += i;
    
    while(i!=-9999)
    {
    i = sc.nextInt();
    sum += i;
    }
    System.out.println(" The sum is " + sum);
    
    You were comparing i to -9999 but only setting i to the first input. You also never added the first input to your sum. Do you have questions with the above code (that should work but I didn't test it).
     
  9. Unread #5 - Feb 6, 2015 at 9:52 AM
  10. Lerker
    Joined:
    Apr 1, 2014
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0

    Lerker Active Member
    Banned

    Basic java while loop help please.

    Yeah, I just figured that out my self, thanks.. I've ended up using this;

    while(i != -9999)
    {
    sum = sum + i;
    i = sc.nextInt();
    }
     
  11. Unread #6 - Feb 6, 2015 at 10:00 AM
  12. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Basic java while loop help please.

    That works fine too!
     
< Would anyone be interested in an open source, extensible RS Client? | Should I learn Objective C or Java first? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site