Results 1 to 5 of 5

Thread: Help with loop

  1. #1
    Scatty is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Help with loop

    I have been given this assignment:
    This method takes a range (i.e., the starting number and the ending number) and a skip number. The method computes the sum of the numbers in the range excluding all numbers that are multiples of the skip number. Note that both start and end are part of the computed sum, unless they are multiples of the skip number. For instance, given input 21, 25 and 4, the method returns 91 (i.e., 21 + 22 + 23 + 25 = 91).

    The following is the method prototype for the method:
    int rangeHash(int start, int end, int skip)

    If end is smaller than start, or if start or end is a negative number, the method signals an error condition by returning -1. Also, whenever skip is less than or equal to 0, the method signals an error condition by returning -1.


    The code I have written:

    import java.util.Scanner;

    public class rangeHash
    {
    public static void main (String[]args)
    {
    int start, end, skip;

    Scanner keyboard = new Scanner(System.in);

    start= keyboard.nextInt();
    end = keyboard.nextInt();
    skip = keyboard.nextInt();
    System.out.println(rangeHash(start, end, skip));

    }

    public static long rangeHash(int start, int end, int skip)
    {int answer = 0;

    if ((start>end)||(start<0)||(end<0)||(skip<=0))
    answer = -1;


    else
    while (start <= end)
    {
    if (start%skip == 0)
    {
    start++;
    System.out.println(start);
    }
    else
    answer = answer + start;
    System.out.println(answer);
    start++;
    System.out.println(start);
    }
    return answer;

    }

    }

    The program compiles fine the only problem is when it hits the first instance where the number is divisible by the skip it stops adding any numbers, even if it is not divisible. Can anyone see what I am doing wrong?I am an absolute beginner.:confused:

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    5

    Default

    % is the remainder operator...
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    You need to add/change some debug statements to your code to see what is happening.
    For example put id strings in ALL of the println()s so you can see which are outputing what. For example:
    Java Code:
               System.out.println("start=" + start + " skipping summing");
    Your logic is basically correct, just one statement too many.

    Also for quicker debugging replace the scanner input with fixed input until you get the logic worked out. For example:
    Java Code:
       start = 21; //keyboard.nextInt();
       end = 25; //keyboard.nextInt();
       skip = 4; //keyboard.nextInt();

  4. #4
    AMaineJR is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Norm is right. Your incrementing your start variable one too many times. Looks like you should be incrementing start no matter what happens with the modulus operation. Also, you could clean up the code a little bit by saying

    Java Code:
    if (start % skip != 0)
     answer += start;
    instead of

    Java Code:
    if (start%skip == 0)
    {
      //Do nothing
    }
    else
       answer = answer + start;

  5. #5
    Scatty is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi. Thanks for the reply, I will give it a try again when I get off work and let you know how it went.

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  2. need a loop to add new row(s)
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 07:21 AM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  4. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 03:29 PM
  5. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •