gpt4 book ai didi

java - 剪刀石头布游戏 3 中最佳 2 循环问题

转载 作者:行者123 更新时间:2023-12-01 11:08:46 28 4
gpt4 key购买 nike

如何让电脑在输入错误(除了选项之外的东西;石头、布、剪刀)时自动获胜?它也需要计算这些。另外,当玩家选择再次玩时,如何删除“欢迎...”再次出现;仅当程序首次启动时才需要。

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!");
}
}
}
}

最佳答案

您始终可以将 if 语句更改为以下内容:

if(UserInput.equals("Rock"))  {
Player = 1;
}
else if(UserInput.equals("Paper")) {
Player = 2;
}
else if(UserInput.equals("Scissors")) {
Player = 3;
}
else {
Player = 0;
}

您已经将其设置为如果玩家 < 1,计算机将自动获胜。

注意:您的while (Player > 3 || Player < 1)语句实际上应该是 if陈述。否则,就会出现无限循环。

要回答你的第二个问题,只需移动语句 System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");在你的循环之上。这样它只会被询问一次:在开始时。

关于java - 剪刀石头布游戏 3 中最佳 2 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32612249/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com