• 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

If statement please help

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am new at learning Java. I really need your help. Actually, I know it is easy for most of you, but I just really lost, can not figure that out. I am supposed to write a code which The program asks the user to enter two integers. Then it asks three questions: what's the product of the two integers, what's the quotient of the two integers, and what's the remainder of the division of the two integers. It inputs the answers to each of the question from the user, tells the user whether the answer was correct or worng, and finally prints a message that depends on how many correct answers the user provided.

I did the beginning, but I can not see the logic of if statements. The program supposed to say wrong or write in accordance with the users' answer. I can not provide wrong or right. How can I fix it? Thank you so much

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

There may be other issues with the code, but these few lines are problematic:


int score1;
score1=a*b;
score1=in.nextInt();
if (score1!=true)


"score1" is an int - you can't compare it with a boolean (which is what "true" is); you can only compare it to another int. Also, the value of score1 is changed by two successive statements, so the first value (a*b) will be lost. I think you mean something like this:


int score1 = in.nextInt();
if (score1 != a *b)



As an aside, this forum is specifically for question about threads and synchronization (see its title). I'll move it to a more appropriate forum.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Your code is difficult to read because you didn't notice the code button. I shall edit your post so you can see how much better it looks.

No, it is not easy for us; we had all sorts of problems when we started.

I would suggest you implement the changes Ulf has suggested, then tell us what happens.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And never write != true or == true or != false or == false. They are all prone to nasty errors if you mistakenly write =.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate on Campbell's last note... writing this is unnecessary:

all you have to write is

assuming that 'something' is a boolean (and if it's not, you'll get an error). The problem with writing it out is that if you forget one of the two '=' characters, you ASSIGN the literal value to your variable, and the if condition will ALWAYS evaluate to your literal.

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:To elaborate on Campbell's last note... writing this is unnecessary:
. . .

And to elaborate on Fred's last note . . . If you want something to be false you never writeAlways write
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and to elaborate on Campbell's elaboration of my elab.... oh... nevermind...

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

 
Fifi Akt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your help. There is no enough word to explain my appreciation.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic