• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java for loop help!!!!!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi I need some help with this.

for(a=1,b=3;a<=97 && b<=99;a+=2,b+=2){
 
   total=0;
total=total+a/b;
System.out.printf("\n Total=%.4f",total);

}

I want to make it so the total would keep adding the previous total. for example, the first would be 1/3 and the second would be the sum of 1/3+3/5......

if there are any links to how to do it or if anyone can help. I have tried to move the total and System.out.printf outside the for loop but it would not work.

thanks
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have made that loop surprisingly complicated. Why don't you consider that one of the numbers is always two more than the other, so you only need one number in the loop header.
Remember you are dealing with integer arithmetic, so I can predict what the division will produce just by looking.
I can see at least three other problems, but maybe if you correct that one, you will be able to correct the others yourself.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jack,

welcome to the Ranch and enjoy the stay!

I would certainly bring the line 'total = 0' before the loop.

The code is alright, but beware of one thing: if a and b are defined as int. then a / b is an integer division, and since a < b, the outcome will always be 0. If so, then this will help: total = total + (double) a / b;
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

To begin with your code will not even compile which makes it difficult for people to pick it up and help you. That said, assuming you do actually have it compiling and running yourself and it's just a cut and paste error, can you describe in a bit more detail in what way "it would not work"?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly don't edit your post so as to make it looks as if we were mistaken. Please post the whole code in a new post.
reply
    Bookmark Topic Watch Topic
  • New Topic