I have the following code which allows a user to play Rock, Paper, Scissors with the computer. The code allows for 10 rounds and counts the number of wins and ties. I need to allow for user misspelling by allowing the input to be accepted if the user types the first two letters of the word correctly ie: roxx, paxxx, scxxxxx. I'm not sure how to go about doing this or even where I would write it into my code.
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
Scanner in= new Scanner (System.in);
String yourTurn;
int computerTurn;
String computerString;
int yourTurnCount = 0;
int computerTurnCount = 0;
int tieCount = 0;
for (int i=1; i<=10; i++)
{
System.out.print ("Enter your play: rock, paper or scissors");
yourTurn = in.nextLine();
Random generator = new Random();
computerTurn = generator.nextInt (3) +1;
if (computerTurn == 1)
computerString = "rock";
else if (computerTurn == 2 )
computerString = "paper";
else
computerString = "scissors";
System.out.println ("Your pick: " + yourTurn);
System.out.println ("Computer pick:" + computerString);
if (yourTurn.equalsIgnoreCase(computerString))
{
System.out.println ("It's a tie!");
tieCount++;
}
else if (yourTurn.equalsIgnoreCase("rock") && computerString.equalsIgnoreCase("paper"))
{
System.out.println("Computer wins!");
computerTurnCount++;
}
else if (yourTurn.equalsIgnoreCase("rock") && computerString.equalsIgnoreCase("scissors"))
{
System.out.println("You win!");
yourTurnCount++;
}
else if (yourTurn.equalsIgnoreCase("paper") && computerString.equalsIgnoreCase("rock"))
{
System.out.println("You win!");
yourTurnCount++;
}
else if (yourTurn.equalsIgnoreCase("paper") && computerString.equalsIgnoreCase("scissors"))
{
System.out.println("Computer wins!");
computerTurnCount++;
}
else if (yourTurn.equalsIgnoreCase("scissors") && computerString.equalsIgnoreCase("rock"))
{
System.out.println("Computer wins!");
computerTurnCount++;
}
else if (yourTurn.equalsIgnoreCase("scissors") && computerString.equalsIgnoreCase("paper"))
{
System.out.println("You win!");
yourTurnCount++;
}
}
System.out.println("Your wins: " + yourTurnCount + "\nComputer wins: " + computerTurnCount + "\nTies: "
+ tieCount);
}
}