WeiJie Lim

Ranch Hand

Posts: 92

posted 4 years ago

"Write a Java Program that prompts the user to enter an integer and checks whether the number is divisible by 3 but not 5. The user will be prompted repeatedly to enter the next number until the user enters 0 as the input. "

I have great trouble trying to satisfy the "number is divisible by 3 but not 5" condition. It doesn't seem possible whichever logical operator I use. There will be certain cases which will slip through.. =/.

If I use ^ operator, numbers such as 15 does not satisfy the condition.

If I use & operator, numbers such as 63 does not satisfy.. argh.

I have great trouble trying to satisfy the "number is divisible by 3 but not 5" condition. It doesn't seem possible whichever logical operator I use. There will be certain cases which will slip through.. =/.

If I use ^ operator, numbers such as 15 does not satisfy the condition.

If I use & operator, numbers such as 63 does not satisfy.. argh.

posted 4 years ago

I hope that you're just exaggerating out of frustration, and that you don't actually consider it a possibility that this is impossible to do?

So you need "Divisible by 3, AND NOT divisible by 5".

Do you know how to do "divisble by X"?

Do you know how to do "NOT (some boolean expression)"?

Do you know how to do "some boolean expression AND some other boolean expression"?

If so then that's all you need.

If you can't get it to work, post an SSCCE(←click) that shows only the code for the failed numbers. No user input, no UI. Just a tiny program like this:

In fact, even if you don't end up posting it here, that's how you should be approaching trying to solve this problem yourself. Get rid of everything that's not directly related to the specific difficulty you're having.

- 1

WeiJie Lim wrote:

I have great trouble trying to satisfy the "number is divisible by 3 but not 5" condition. It doesn't seem possible whichever logical operator I use. There will be certain cases which will slip through.. =/.

If I use ^ operator, numbers such as 15 does not satisfy the condition.

If I use & operator, numbers such as 63 does not satisfy.. argh.

I hope that you're just exaggerating out of frustration, and that you don't actually consider it a possibility that this is impossible to do?

So you need "Divisible by 3, AND NOT divisible by 5".

Do you know how to do "divisble by X"?

Do you know how to do "NOT (some boolean expression)"?

Do you know how to do "some boolean expression AND some other boolean expression"?

If so then that's all you need.

If you can't get it to work, post an SSCCE(←click) that shows only the code for the failed numbers. No user input, no UI. Just a tiny program like this:

In fact, even if you don't end up posting it here, that's how you should be approaching trying to solve this problem yourself. Get rid of everything that's not directly related to the specific difficulty you're having.

WeiJie Lim

Ranch Hand

Posts: 92

It is sorta covered in the JavaRanch Style Guide. |