Sunday 15 August 2010

java - String input doesn't work in if-else statement -


I'm new to Java, and I created a mini-game of some sort, and I wanted to pick players Whether they want to play again, I tried to change my initial boolean variable to a fixed type and add some lines of code, but every time I play a game, the code does not work, it was asked to me that Do I want to play again or not, but Sya is that although I finally typed "yes" to the console, do not restart the game. Anyone can help me please, I would really appreciate, thank you!

  import java.util.Random; Import java.util.Scanner; // Rocks, papers, scissors, compliance with the game; Public square made by William Two RockPaperScissor {Fixed int gameplay = 0; Stable boolean start = true; Public static zero main (string [] args) {while (start) {@SuppressWarnings ("resource") scanner userInput = new Scanner (System.in); Random comOutput = new random (); System.out.println ("Do you choose rock, paper or scissors?"); String User Chice = User input.exactine (); Int comChoice = comOutput.nextInt (2); Switch (User Chauses) {Case "Rock": If (Commutation == 0) {System.out.println ("I choose rock too! It's a draw!"); } And if (comChoice == 1) {System.out.println ("I will choose paper! I have won!"); } And {System.out.println ("I choose scissors! You won!"); } break; Case "Paper": if (comChoice == 0) {System.out.println ("I chose rock! You won!"); } And if (comChoice == 1) {System.out.println ("I also select the paper! This is a draw!"); } And {System.out.println ("I choose a scissor! I win!"); } break; Case "Scissors": If (Comchez == 0) {System.out.println ("I choose rock! I'll win!"); } And if (comChoice == 1) {System.out.println ("I choose the paper! You won!"); } And {System.out.println ("I also select scissors! It's a draw!"); } break; Default: System.out.println ("I do not think this is an option."); break; } Gameplay ++; System.out.println ("You've Played" + Gameplay + "Games"); // The bottom is part I want to ask, what's wrong? System.out.println ("Do you want to play again?"); String PlayAgn = user input.net (); If (playAgain == "Yes") {System.out.println ("Game Restart ..."); Start = true; } And if (PlayAgain == "No") {System.out.println ("Skip Game"); Start = wrong; }}}}   

Print out of Eclipse:

  Do you choose rock, paper or scissors? Rock ** // my input ** i choose paper! I won! You played 1 game Do you want to play again? Yes ** // My Input ** Do you choose rock, paper or scissors? Paper ** // My Input ** I also select paper! This is a draw! You played 2 games Do you want to play again? No ** // my input ** Do you choose rock, paper or scissors?    

to replace the comparison between the == the wanted. So this:

  if (playAgain == "yes") {System.out.println ("restarting game ..."); Start = true; } And if (PlayAgain == "No") {System.out.println ("Skip Game"); Start = wrong; }   

To create it:

  if (playAgain.equals ("yes")) {System.out.println ("Restart the game. ");); Start = true; } And if (playAgain.equals ("no")) {System.out.println ("Skip play"); Start = wrong; }   

To work properly.

However, I suggest using it, because is equal with , the comparison is case-sensitive, so if the user input yes or < Code> yEs , playAgain.equals ("yes") back false

UPDATE

As Maroun Maroun says, it is better to use

  "yes" .equals (playAgain)   

and

  "no" .equals (playAgain)   

Because playAgain is null this is a null pointer exponent < / P> not throw Not will

No comments:

Post a Comment