• Post Reply Bookmark Topic Watch Topic
  • New Topic

int's and if statement help!  RSS feed

 
Mike orr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I'm trying to write a quick program that tests out some stuff i've made for a text based game. i wanna test a simple stat system, a simple lvl up system, and a simple combat system. I'm trying to make it where the player is asked what race he/she wants to play then depending on the answer it changes the players starting stats. however after'v- doing what i thought would work and trying several different variations of what i thought would work i've come to the conclusion that I'm stuck! YAY progress! below is the code i've got typed. again I'm trying to keep it simple also I'm new to programing so i greatly appreciate your help (insert your name here.).

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first point. Never use == to compare Strings. You need to use the equals() method. == checks to see if they are the same object, but you want to check if they have the same value.

Once you've done that, you might want to consider putting the choice in a loop. At the moment, if I entered "Orc" (or "Banana", or whatever) it would let me do that, but wouldn't initialise the character correctly.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags ( <-- click on link for instructions ) and keep line lengths to a reasonable length. 80 columns is a typically acceptable length. I've adjusted both for you this time.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import statement between code ??
poor coding ...
 
Mike orr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first thanks for the edit greg, i will be sure to do that in the future.
second i knew there was something i was forgetting with strings and comparing them. so i should use

yep that seems to work much better!

the loop was the next thing i was gonna work on i just got side tracked. thank you guys so much for the help. take care!
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:import statement between code ??
poor coding ...
No, it means two classes have been put into one set of code tags. There would be no such error in the original code.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike orr wrote:first thanks for the edit greg, i will be sure to do that in the future.
second i knew there was something i was forgetting with strings and comparing them. so i should use yep that seems to work much better!

Little tip for you: when comparing with String literals, it's quite a good idea to invert the comparison, ie:
if ("Elf".equals(player.race))
instead of
if (player.race.equals("Elf"))
because then you don't get a NullPointerException if player.race is null.
Oh, and you almost NEVER want to put a semicolon after an if statement.

BIG tip for you: Separate your GUI code from your game logic completely - or as far as you possibly can. GUI code should ONLY be concerned with display and interaction; it shouldn't be doing any calculations - and that includes stats updates. Keep all that stuff separate and simply hand back results for your GUI to display.

HIH

Winston
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Nikhil Sagar wrote:import statement between code ??
poor coding ...
No, it means two classes have been put into one set of code tags. There would be no such error in the original code.


Oh !! if it is then, I am sorry Mike...
Thanks Sherrif....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!