User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Java Help Page [1]  
JCash
All American
988 Posts
user info
edit post

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

BIGcementpon
Status Name
11318 Posts
user info
edit post

Looks like the divisor isn't getting incremented because it's in the if statement.

2/13/2008 1:00:21 PM

agentlion
All American
13936 Posts
user info
edit post

1) is this for a class? if so, don't bother asking here
2) 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

JCash
All American
988 Posts
user info
edit post

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

agentlion
All American
13936 Posts
user info
edit post

Quote :
"I understand no wants to do someone else's homework"

it's not that people here don't want to do that, it's that if this is for a CSC class at NCSU it is a clear and explicit violation of the ethics and cheating rules. If it's for a non-degree or credit only class at UNCA, well i guess there's no much harm.

but your main question has already been answered. Your divisor++ is inside the if statement, so as soon as that if statement resolves to false, divisor is never incremented again and you're stuck in an infinite while loop. You just need to move divisor++ outside the if statement brackets so it is executed every time the while loop runs.

of course, a for loop would be better suited for this task anyway


for (divisor = 100; divisor <= 1000; divisor++) {
if (divisor % 5 == 0 && divisor % 6 == 0)
System.out.println(divisor);
}

2/13/2008 1:18:38 PM

JCash
All American
988 Posts
user info
edit post

^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

afripino
All American
11425 Posts
user info
edit post

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

JCash
All American
988 Posts
user info
edit post

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

WolfAce
All American
6458 Posts
user info
edit post

just think through the loop and figure where you need to do the counting to ten, and println("\n"); should be fine

I 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

DirtyMonkey
All American
4269 Posts
user info
edit post

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

JCash
All American
988 Posts
user info
edit post

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

JCash
All American
988 Posts
user info
edit post

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

WolfAce
All American
6458 Posts
user info
edit post

^^^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 much

the api is your friend
http://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

Metricula
Squishie Enthusiast
4040 Posts
user info
edit post

dude

do your own homework

2/17/2008 9:53:57 PM

 Message Boards » Tech Talk » Java Help Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.