• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Homework Question  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was assigned my first homework assignment for Intro to Java, and it is to write a program that counts form 1 to 100 in increments of 5. If a number is divisible by 25, print the percentage of 100 represented by that number (Example 25=25%, 50=50%, and so on). I have figured out how program the counting part by increments of 5 but I cant seem to figure out how to get the percentages to display... Any ideas? Any help is much appreciated

Here is my code:

 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing to add is an if statement. After all, you want to do something only if count has some specific value.
To check for divisibility, use the % operator.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I put your code into code tags - see how it makes it easier to read? and I see Rob has beaten me to giving you a tip...I have also re-formatted it...You used both tabs and spaces to indent...see this for a discussion on that (hint - don't do it).

I also moved around your braces. You sort of combined two different ways of where to put them. Your braces for the main method were on their own line, but stayed on the same like as the while...I would strongly suggest picking one style or the other, and stick to it. Many folks have STRONG opinions on which is 'correct', but the important thing is to be consistent in how you do it.
 
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program seems good to me....
you just need a condition that if count is fully dividable with 25 then print % sign as well.
Look how you can put condition in java program...
The syntax for IF condition is as :
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
although a 'while' loop will work fine, when you know how many iterations you need a 'for' loop is more appropriate.
 
Joe Mathieu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone who helped me out! I now have the program just the way it was supposed to be for full credit. As far as my programming style, I think we'll be getting into that here in a few chapters. I'm very new not to mention, a novice to say in the least, at programming but this forum is definitely a huge help. Thanks everyone again!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!