This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
hi guys our class professer gave us a question that was to create a program that loops from 1 to 50 and prints each value on a seperate line .also print foo for every multiple three line,bar for every multiple of five and baz for every multiple of seven.So the program i wrote was like this
the console output was like this:
15foo...i want to display it as 15 foo bar
15bar..i want to display it as 15 foo bar
so please let me knw wats da modifications need 2 be made
I would assume that you want "21 foo baz" in the output too since 21 is a multiple of 3 and 7. Instead of having 3 places that you output the text create a text variable at the beginning of each loop in your for loop, then add text to it for each case that is met and output the text at the end of the loop. If you'd like it to perform better (since Strings are immutable and traditional String concatenation wastes memory) use a StringBuilder. Let me know if you need more guidance.
Joined: Jul 31, 2009
Also consider using the Modulus operator to do your comparison: Modulus. It should make your code a lot simpler.
Jarred Olson wrote:IInstead of having 3 places that you output the text create a text variable at the beginning of each loop in your for loop, then add text to it for each case that is met and output the text at the end of the loop.
That was my first thought as well. Then I thought - why not just change all the System.out.println()s to System.out.print(). Print the value of i at the start of the loop instead of in each S.o.p and print a new line at the end of the loop.
welll fred i dont know whether it is complicated logic but i am developing my logic and the logic i have applied is giving me correct output but with a slight difference(almost 99% correct) ...yeah i heard about modulus it is used for deriving remainders so how can that be of such help in this case my brain not working in terms of modulus
I think you are printing i on every if statement and also doing a println. That's the cause. Print the number to be checked once and let the printing job be done in the check statements. Also using modulo will be much better and storing the multiples in an array and looping it. Well think about it.... Also make the ifs more compact using the short circuit operator &&. It would look much better
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
I never said constructors. I hope you have tried taking in parameters from the command line and displaying it in the java program. These command parameters go into String args from the main method. So if you pass any number like 1 2 3 in the command line, we can access it as arg and so on. Also in your program the starting if statement which has a m, Why do you want to do that?
Joined: Jul 20, 2009
hi just use the '%' (modula) operator .
in your method first print 'i ' as it is
then check i%3 == 0
then check i%5 == 0 print bar
then check i%7== 0
then print space .
Joined: Jul 31, 2009
Modulus is easy to use and helpful once you grasp the concept. Think of it as division (easy to do since the symbols are similar / vs %). Only Modulus returns the remainder as an int. Examples:
This will help you to determine when you have a multiple of another number. If x % i = 0, then i is a multiple of x. Hope this helps with Modulus... and your homework