I'm trying to write a program that will display all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6.I figured I would do the ten per line part last, but if anyone can point out where my code has gone wrong for the rest, please let me know. int divisor = 100; while (divisor >=100 && divisor <= 1000) { if (divisor % 5 == 0 && divisor % 6 == 0) { System.out.println(divisor); divisor++; } }
2/13/2008 12:54:23 PM
Looks like the divisor isn't getting incremented because it's in the if statement.
2/13/2008 1:00:21 PM
1) is this for a class? if so, don't bother asking here2) you say "code has gone wrong for the rest", what exactly has gone wrong with it[Edited on February 13, 2008 at 1:02 PM. Reason : .]
2/13/2008 1:01:50 PM
Yes, it's for a class.I understand no wants to do someone else's homework, I'm taking the class to try and actually learn a little about programming. I graduated from NCSU last semester w/ two degrees, but I'm taking this class non-degree at UNCA before I start work in June, so I could care less about the grade.If no one wants to help still, that is fine, I'll keep plugging away different things, I'm just a little stuck and out of ideas until class at 3.
2/13/2008 1:12:17 PM
for (divisor = 100; divisor <= 1000; divisor++) { if (divisor % 5 == 0 && divisor % 6 == 0) System.out.println(divisor);}
2/13/2008 1:18:38 PM
^you're right, i probably should have clarified that first.i was a little confused on the for loops from the book, but that does make more sense now. thanks for the help.
2/13/2008 1:46:10 PM
yeah, just place the divisor++ outside of the while.in your method:while (divisor >=100 && divisor <= 1000) {if (divisor % 5 == 0 && divisor % 6 == 0) {System.out.println(divisor);}divisor++;}you can also use a do-while:int divisor = 100;do{if (divisor % 5 == 0 && divisor % 6 == 0) {System.out.println(divisor);}divisor++;}while(divisor >= 100 && divisor <= 1000); of course, the for loop is definitely the best way to go about this. just throwing out an option.[Edited on February 13, 2008 at 7:29 PM. Reason : ]
2/13/2008 7:20:29 PM
Now I'm stuck on the ten per line part. I'm assuming I need to declare an int = count, and count++ for each print?Do I need to put another if statement right after the first to count up to 10 each time before I start a new line?And should I use a different command other than the println to format the output?
2/13/2008 9:53:46 PM
just think through the loop and figure where you need to do the counting to ten, and println("\n"); should be fineI find that trial and error and spamming informative printlns throughout your code for crude debugging is the best way to start learning how to code, unless you're just having syntactical trouble[Edited on February 13, 2008 at 10:06 PM. Reason : ]
2/13/2008 9:56:00 PM
it's been a really long time since i've done any java, but i thought that System.out.println() automatically put a new line at the end of your string. if that's the case, shouldn't you be using System.out.print() for numbers 0-9 and System.out.println() for the 10th number per line?
2/13/2008 10:20:48 PM
I haven't been able to figure out how to put more than one number on a line yet...so that might explain why. But its certainly possible I'm just screwing something up.
2/13/2008 10:25:01 PM
ok, looks like i got it w/ this:int count = 0; for (int divisor = 100; divisor <= 1000; divisor++){ if (divisor % 5 == 0 || divisor % 6 == 0) { count++; if (count % 10 == 0) { System.out.print(divisor + " " + "\n"); } else System.out.print(divisor + " ");but is there an easier way any of you can think of? just curious for future reference.
2/13/2008 10:28:48 PM
^^^no he's right, it's been a pretty long time since I did any java too, or at least any where the nature of print statements mattered that muchthe api is your friendhttp://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html#println(java.lang.String)so yeah printlns are going to terminate the line at the end so don't use that until your tenth number, or just print a '\n' instead^the first thing I thought of was a separate counter variable that incremented to ten and if the counter had iterated ten times print a new line and reset the counter to zero, but if this were in C you'd probably be saving memory by doing it your way (less variables on the stack), doesn't really matter that much with java though[Edited on February 13, 2008 at 10:36 PM. Reason : ]
2/13/2008 10:31:55 PM
dudedo your own homework
2/17/2008 9:53:57 PM