• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

i want to have two arguments in an if statement

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm writing a program that takes a percent chance of giving random numbers, int y and int x, in my program the if statement works if I just have the y argument but doesn't run with the x argument too. it says "else without if" and
"this assigned value never used please help !!!

 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always use braces in if statements (and proper indentation).

This:


is equal to this:



so, of course you get "else without if".

Also, please only post the relevant code. All the GUI related code is irrelevant. Read: http://sscce.org/
 
mitchell cooper
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did that still getting errors !
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The end braces are in the wrong places. This code...

should be...

that is, the braces should enclose all statements that are affected by the "if".
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mitchell,

the resulting clauses of the "if" statement are single line constructs, that is you can have one command affected by the "if" clause.

it has it's uses, but it's not very useful, so we need to have a way to make the clauses have more than one command. We do that with code blocks or scopes which in Java are defined by "{ }" curly braces. Everything in the curly brace is in the same scope, and in the case of the "if" clause, the code block can include as many commands as you need.

so remember to put all the code you need in the specific block you want it in.

Please note: the if/else structure does not allow you to have anything between the true and false clauses, once you put another line of code after that true clause, the else block is assumed to be discarded, unless specifically defined with the else. You do that in multiple places.

mitchell cooper wrote:did that still getting errors !

 
Don't listen to Steve. Just read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic