Monday 15 February 2010

java - Can't figure out why my method isn't executing -



java - Can't figure out why my method isn't executing -

this question has reply here:

how compare strings in java? 23 answers

below snippet of code class in simple programme i'm writing plays simple game of blackjack. i don't understand why nail method isn't executing whenever, upon execution of program, come in "hit". goes else part of statement every time regardless of enter. added system.out.println statement create sure strings matched. sense must making basic error can't seem figure out.

system.out.println("would nail or stand?"); scanner input = new scanner(system.in); string playerdecision = input.nextline(); //system.out.println(playerdecision); if(playerdecision == "hit") { hit(); } else { system.out.println("error"); } } public void hit(){ player.makehand(deck.draw()); system.out.println("you have next cards: "); player.gethand(); system.out.println("your hand total "); system.out.println(player.findhandtotal());

}

wrong string comparison. try

if("hit".equals(playerdecision)) {

java if-statement methods

No comments:

Post a Comment