Results 1 to 5 of 5
Thread: Help with loop
- 05-17-2010, 07:52 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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:
- 05-17-2010, 08:11 PM #2
% is the remainder operator...
:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 05-17-2010, 09:34 PM #3
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:
Your logic is basically correct, just one statement too many.Java Code:System.out.println("start=" + start + " skipping summing");
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();
- 05-18-2010, 07:10 AM #4
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
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
instead ofJava Code:if (start % skip != 0) answer += start;
Java Code:if (start%skip == 0) { //Do nothing } else answer = answer + start;
- 05-18-2010, 07:23 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
need a loop to add new row(s)
By doha786 in forum New To JavaReplies: 1Last Post: 02-03-2010, 06:21 AM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10-20-2008, 02:29 PM -
while loop
By michcio in forum New To JavaReplies: 5Last Post: 01-27-2008, 12:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks