Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question seems undoable ?  RSS feed

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, you are right, I should be more concise in the way I post my code. Unneeded portions should be shaved off..

Yeah, I think this might be it



for checking the condition of "divisible by 3 but not by 5" .
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:Yeah, I think this might be it



Okay, so now take that SSCCE skeleton I gave you, and plug in some samples, like, say, 3,4,5,6,8,9,10,14,15,16, and see if you get the right results.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!