所以我的代码全部正常工作,但是它没有计算第一轮,所以我需要首先刷新缓冲区。所以我试着放置:
in.nextln();清除缓冲区但它仍然希望有用户输入,因此它会更加混乱。
我试着将它放在这个位置:
//Gets the users play of rock paper scissors
System.out.println("Your play:");
//try adding in.nextLine(); here to flush buffer
userPlay = in.nextLine();
以下是我的完整代码,可帮助您更好地了解我正在做的事情:
import java.util.Scanner;
import java.util.Random;
public class final5
{
public static void main(String[] args)
{
//Intitalizing all variables
String userPlay;
String computerGuess = "";
int numGames = 0;
int userWins = 0;
int computerWins = 0;
int computerRandom; //Makes the computer have random guess
String response;
Scanner in = new Scanner(System.in);
Random r = new Random();
System.out.println("Welcome to Rock Paper Scissors");
System.out.println("Number of rounds:");
numGames = in.nextInt();
//Checks to make sure num of games is valid/odd
while ( numGames % 2 == 0 )
{
System.out.println("Please ensure your number is odd, and try again:");
numGames = in.nextInt();
}
in.nextLine();
System.out.println();
System.out.println("To play, please use the following inputs: (Rock = R, Paper = p, Scissors = s)");
for (int i = 1; i <= numGames; i++)
{
//Gets the users play of rock paper scissors
System.out.println("Your play:");
userPlay = in.nextLine();
while (!userPlay.equalsIgnoreCase("R") && !userPlay.equalsIgnoreCase("P") && !userPlay.equalsIgnoreCase("S"))
{
System.out.println("Sorry, that is invalid. Please enter a a valid entry: Rock (R), Paper (P), Scissors (s)");
userPlay = in.nextLine();
}
//We can intialize the userplay to all cap to make the code easier to read lower
userPlay = userPlay.toUpperCase();
computerRandom = r.nextInt(3)+1;
//Grabs a random number from random generator and italizes it to play rock, paper, or scissors
if (computerRandom == 1) computerGuess = "R";
else if (computerRandom == 2) computerGuess = "P";
else if (computerRandom == 3) computerGuess = "S";
if (userPlay.equals(computerGuess))
{
System.out.println("You guys both tied, try again!");
numGames++;
}
//Both tie no win for either
if (userPlay.equals("R"))
{
if (computerGuess.equals("S"))
{
System.out.println("You won, computer had scissors.");
++userWins;
}
}
//If user is rock and beats scirssors
if (userPlay.equals("S"))
{
if (computerGuess.equals("P"))
{
System.out.println("You win, computer had paper!");
++userWins;
}
}
else if (computerGuess.equals("P"))
{
System.out.println("You lost, computer had paper.");
++computerWins;
}
else if (userPlay.equals("P"))
{
if (computerGuess.equals("S"))
{
System.out.println("You lost, computer had scissors.");
++computerWins;
}
}
else if (computerGuess.equals("R"))
{
System.out.println("You win, computer had rock.");
++userWins;
}
else if (userPlay.equals("S"))
{
if (computerGuess.equals("P"))
{
System.out.println("You win, computer had paper.");
++userWins;
}
}
else if (computerGuess.equals("R"))
{
System.out.println("You lose, computer had rock.");
++computerWins;
}
}//end of for loop
//Gives total wins/loses and dermines winner
if(userWins > computerWins)
{
System.out.println();
System.out.println("Your total wins: " +userWins );
System.out.println("Total Computer Wins: " + computerWins );
System.out.println("You have won the computer, congrats!");
}
if(userWins < computerWins)
{
System.out.println();
System.out.println("Your total wins: " +userWins );
System.out.println("Total Computer Wins: " + computerWins );
System.out.println("Magically the computer won, nice try!");
}
}//end of main code
}//end of program
此外,当我清空缓冲区时,它不会在最后添加总胜利,让用户知道他们是赢还是输。