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 3 of 3

Thread: Can't get the loop to work properly

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Can't get the loop to work properly

    *****Solved it myself, thanks for help*****



    Hey there,
    I've got a program which I need to test whether an integer n is the exact sum of k consecutive integer numbers starting from 1.
    eg. 6 is 1 + 2 + 3 and therefore k = 3 and if I put in 4 then it returns false. But what I've only able to achieve so far is to get the program to add numbers 1 to n together, so if I enter 6 I get 21 because 1 + 2 + 3 + 4 + 5 + 6 = 21, but this isn't what I need the program to do. :/ I'm kinda stuck, I've tried a few other things but they aren't working.

    import java.util.Scanner;
    public class ExactSum {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Type an integer:");
    	    int n = keyboard.nextInt();
     
    	    System.out.println("The number "+n+" is not an exact sum");
    	    } 
    	      else {System.out.println("The number "+n+" is the sum of integers from 1 to "+k+" ");
                  }
     
    	    }
    	}

    Any help is appreciated!
    Last edited by thegreatzo; August 10th, 2012 at 10:06 AM. Reason: Solved


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can't get the loop to work properly

    My advice would be to drop the code for now. Back up and make a pseudocode. Once you get the ideas down, turning that into code is much easier.

    To start you off:

    You need to test whether an integer n is the exact sum of k.
    So you need to get two inputs, both of which should be an integer value greater than 0.

    You can break these following steps down more:
    do some math..
    decide what to output


    Once you get each step worked out, go over the steps again and see if you can break any one step down into more than one smaller step. Like getting an integer greater than zero could involve multiple steps which might include:
    -output to user what information you want
    -get an integer back
    -verify the integer is greater than zero

    Hope that gets you back under way

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't get the loop to work properly

    Also posted (and answered) at Need help with loop
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  2. [SOLVED] cannot get this method to work properly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 2nd, 2011, 12:00 PM
  3. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM
  4. I cannot get boolean to work properly
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 18th, 2011, 07:37 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM

Tags for this Thread