gpt4 book ai didi

java - 我使用 Java 用一种方法编写了一个 "Rock, Paper, Scissor, Shoot"游戏。我需要帮助循环程序

转载 作者:行者123 更新时间:2023-12-01 19:07:42 25 4
gpt4 key购买 nike

我可以循环程序,但每次输入一个值时,它都会返回 2 个值,用户获胜和用户失败。我尝试过使用多种方法并创建一个新类作为测试器,但在弄清楚逻辑时遇到了一些问题。至于循环,我尝试过使用 for 循环、while 和 do while。

提前致谢!

<小时/>
// Rock Paper Scissor Shoot Game

import java.util.Random;
import java.util.Scanner;

public class RockPaperSciccor {
public static void main(String[] args){

int wins = 0;
int losses = 0;

int rnd;


for(rnd=0;rnd<=10;rnd++)
{

Random GAME = new Random();
int PC = 1+GAME.nextInt(3);

Scanner input = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
SCISSOR = 1;
ROCK = 2;
PAPER = 3;

System.out.println("");
System.out.println("Choose Your Weapon! ");
System.out.println("1 = Scissor| 2 = Rock| 3 = Paper");
System.out.println("");
int USER = input.nextInt();

while (USER > 3) {
System.err.println("Incorrect value entered, fool");
System.err.println("Choose a number 1-3");
return;
}
System.out.println("___________________");

if(USER == PC){
if(USER == SCISSOR){
System.out.println("You Both Played Scissor");
}
if(USER == ROCK){
System.out.println("You Both Played Rock");
}
if(USER == PAPER){
System.out.println("You Both Played Paper");
}
System.out.println("Draw");
System.out.println("___________________");
System.out.println("Wins: " + wins + "| Losses: " + losses);
}
//User wins
if(USER == SCISSOR && PC == PAPER){
System.out.println("You: Scissor");
System.out.println("PC: Paper");
System.out.println("Scissor Cuts Paper");
System.out.println("You Win!");
System.out.println("___________________");
wins++;
System.out.println("Wins: " + wins + "| Losses: " + losses);
}
//Pc wins
else if(PC == ROCK){
System.out.println("You: Scissor");
System.out.println("PC: Rock");
System.out.println("Rock Breaks Scissor!");
System.out.println("PC Wins!");
System.out.println("___________________");
losses++;
System.out.println("Wins: " + wins + "| Losses: " + losses);
}
//User wins
if(USER == ROCK && PC == SCISSOR ){
System.out.println("You: Rock");
System.out.println("PC: Scissor");
System.out.println("Rock Breaks Scissor");
System.out.println("You Win! ");
System.out.println("___________________");
wins++;
System.out.println("Wins: " + wins + "| Losses: " + losses);
}
//Pc wins
else if (PC == PAPER){
System.out.println("You: Rock");
System.out.println("PC: Paper");
System.out.println("Paper Covers Rock!");
System.out.println("PC Wins!");
System.out.println("___________________");
losses++;
System.out.println("Wins: " + wins + "| Losses: " + losses);
}
//User Wins
if(USER == PAPER && PC == ROCK){
System.out.println("You: Paper");
System.out.println("PC: Rock");
System.out.println("Paper Covers Rock");
System.out.println("You Win!");
System.out.println("___________________");
wins++;
System.out.println("Wins: " + wins + "| Losses: " + losses);
}
// Pc Wins
else if (PC == SCISSOR){
System.out.println("You: Paper");
System.out.println("PC: Scissor");
System.out.println("Scissor Cuts Paper!");
System.out.println("PC Wins!");
System.out.println("___________________");
losses++;
System.out.println("Wins: " + wins + "| Losses: " + losses);
}


}





}
}

最佳答案

您的问题是您的 else/if 选择 block 。每次,即使选择已通过,您也允许其继续。你也没有正确检查。在您使用的每个 if/else 对中,您检查

if(user X && pc Y) ...
else if (pc Z) ...

如果用户玩 X 和 PC Y,或者如果 PC 玩 z,这将评估两个 block 之一。您应该将其设置为:

if(user X)
if(pc Y) ...
else if (pc Z) ...

或者

if(user X && pc Y) ...
else if(user X && pc Z) ...

我建议前者。它的效率稍高一些,因为它只检查用户 X 一次。这不会产生一半明显的差异。

你应该这样做:

if(user plays paper)
if(pc plays rock)
...
else if (pc plays scissors)
...
else if(user plays rock)
if(pc plays scissors)
...
else if (pc plays paper)
...
else if(user plays scissors)
if(pc plays rock)
...
else if (pc plays paper)
...

关于java - 我使用 Java 用一种方法编写了一个 "Rock, Paper, Scissor, Shoot"游戏。我需要帮助循环程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9441051/

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