代码如下。我的程序运行得很顺利,除了两个问题。它不会只玩三轮,我需要它只玩三轮,并根据在三轮中至少赢一次的人来确定胜利者,尽管是平局(如果它随机归结到那个场景)。它会一直播放,直到玩家或计算机的中奖比率达到2:1。我不希望它像这样持续运行。另一个问题是,一旦最终获胜者被宣布,“进入石头,布或剪刀”这一行再次出现,那里不需要它。我该如何着手解决这两个问题?谢谢。
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Prompt user to enter rock, paper, or scissors
//Compare random value
while(true){
int wins = 0;
int losses = 0;
int rnd = 0;
int USER = 0;
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"
+ "Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
// Plays 3 rounds before terminating
while(rnd<3) {
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
Scanner keyboard = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
String USER_Input = keyboard.next();
if(USER_Input.equals("Rock")) {
USER = 1;
}
if(USER_Input.equals("Paper")) {
USER = 2;
}
if(USER_Input.equals("Scissors")) {
USER = 3;
}
//If the user enters a value greater then 3 or less than 1 it will terminate the program
//and display an error message
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered.");
System.exit(0);
break;
}
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if(PC == ROCK){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("Rock v Scissor! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if (PC == PAPER){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("Paper v Rock! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer Wins
else if (PC == SCISSOR){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
}
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YN = new Scanner(System.in);
String YN_String = YN.next();
if(YN_String.equals("Yes") || YN_String.equals("yes")){
}if(YN_String.equals("No") || YN_String.equals("no")) {
System.out.println ("Goodbye!");
break;
}
}
}
}发布于 2015-09-15 11:37:45
代码看起来应该按照您想要的方式工作,除了在平局的情况下,您不会递增四舍五入计数器:
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
rnd++;
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
rnd++;
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
rnd++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}顺便说一句,您可以从每个单独的结果中取出rnd++;,并在每次从用户获得输入时增加它,因为您希望它无论如何都要增加。
您的第二个问题源于这样一个事实,即在您告诉用户结果(包括结束结果)之后,您每次收到输入时都会提示输入新的输入。您可以通过从各个结果中删除提示命令并将其放在while循环的开头来修复此问题。请注意,如果这样做,您可能还希望从欢迎消息中删除提示,否则它将在开头提示两次:一次是在欢迎消息中,另一次是在while循环之后立即启动时。
发布于 2015-09-16 22:54:34
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Set integers for wins, losses, rounds, and user
while(true){
int wins = 0;
int losses = 0;
int round = 0;
int Player = 0;
//Plays 3 rounds before terminating
//Prompt user to input Rock Paper Scissors
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
Scanner keyboard = new Scanner (System.in);
while(round<3) {
System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\"");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
String UserInput = keyboard.next();
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
//If the user enters a value greater then 3 (Scissors) or less than 1 (Rock)
//it will terminate the program and display an error message
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(Player == Computer){
if(Player == Scissors){
System.out.println("Scissors v Scissors! Tie!");
round++;
}
if(Player == Rock){
System.out.println("Rock v Rock! Tie!");
round++;
}
if(Player == Paper){
System.out.println("Paper v Paper! Tie!");
round++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish the various winning scenarios using if and else if statements
//Player wins
if(Player == Scissors)
if(Computer == Paper){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
System.out.println("Rock v Scissors! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
System.out.println("Paper v Rock! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
//Determine final winner using if statements
//Ask if player would like to play again by setting up string
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YesNo = new Scanner(System.in);
String YesNo_String = YesNo.next();
if(YesNo_String.equalsIgnoreCase("yes")) {
}if(YesNo_String.equalsIgnoreCase("No")) {
System.out.println ("Goodbye!");
}
}
}
}https://stackoverflow.com/questions/32576797
复制相似问题