• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's wrong with this code?  RSS feed

 
Shiri Iv
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For some reason for anything I do that is valid, it writes 'wrong choice'... What did I do wrong?



Thanks for helping me!
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shiri Iv,

I've added code tags for you. All the time use them, even when you post short piece of code. Now it is the way easier to read your code.

After I edited your code, is code formatting and indentation looks the way it should?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a good look at the else clause. What is it the "else" of?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you'll need elseif statement after if statement if your checking more than one condition.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't an elseif keyword in Java, only else if.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh my mistake, thanks for pointing it out
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, "else" statement, if omitted "curly" brackets, belongs to the nearest "if" statement, so, always use brackets to avoid the "dangling else" problem.

So, how would you change your code?
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better use statement. your code is running as per follows :
First it will check outer if condition i.e.
and if it returns true it will jump to inner if conditions.
In case of even any if condition returns true except the last one , program continues to check other if conditions too
It will only return correct output to last if condition i.e. to

if last if condition returns false it will output wrong choice even if any other if condition returns true.

Run this program and check yourself

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!