Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 negative numbers and loops
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
slider203




PostPosted: Mon Mar 01, 2010 8:03 pm   Post subject: negative numbers and loops

Hello I need to make a program which starts at -9 adds -5 each time throught the loop and exits when the loop is at -200
heres my code:

class ngtvLoop
{
public static void main (String args [])
{
int negatvNum = -9;
final int Max = -200;
final int factor = -5;
int sum = 0;

while (sum < Max)
{
negatvNum ++;
sum = negatvNum + factor;
System.out.println (sum);
}
}
}

when I run it nothing happens anyone know what Im doing wrong I have a feeling its the negative numbers because I tried the program with just 9 and 5 and it work fine. Any help is greatly appreciated.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Mar 01, 2010 8:08 pm   Post subject: RE:negative numbers and loops

Since when is -9 smaller than -200. Think number line buddy.

-200, ..., -9, ..., -5, ... ,0 , 1, 2, 3, 4, ...

PS: it's this line: while (sum < Max)

Edit: wait, your also increment another thing as well. After 14 interations of the loop, your loop will be only going upward.
slider203




PostPosted: Mon Mar 01, 2010 8:10 pm   Post subject: RE:negative numbers and loops

also Its the ++ line shouldnt it be --?
slider203




PostPosted: Mon Mar 01, 2010 8:13 pm   Post subject: Re: RE:negative numbers and loops

Zren @ Mon Mar 01, 2010 8:08 pm wrote:


Edit: wait, your also increment another thing as well. After 14 interations of the loop, your loop will be only going upward.


sorry I dont get how that happens... your right I saw that happen when I ran the program I just dont see the error in the code
Zren




PostPosted: Mon Mar 01, 2010 8:24 pm   Post subject: Re: negative numbers and loops

Best debugging tool is System.out.print(). Just print out the varibles for every interation (including the ones used in your logic).

But I'll tell you what's happening anyways...


negatvNum ++;
sum = negatvNum + factor;

pass 1:
negatvNum = -9 ++ = -8
sum = -8 + -5 = -13

sum = -13

pass 2:
negatvNum = -8 ++ = -7
sum = -7 + -5 = -12

sum = -25

pass 3:
negatvNum = -7 ++ = -6
sum = -6 + -5 = -11

sum = -36

I also think you meant to have sum += factor.

As you can see, negatyNum is slowly going positive. After 14 passes, it will equal +5. At that time when you add negatvNum and factor together, it equals 0. And from then on, sum will be incremented by a positive number thus going upwards.

Why do you need negatyNum anyways? Take it out and you'll have the desire effect. -9, -14, ..., -204

I suggest the use of For Loops.
for (int i = 6; i > 140; i += 5) {}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: