• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question seems undoable ?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic