gpt4 book ai didi

Java石头/剪刀/布,这样做错了吗?

转载 作者:行者123 更新时间:2023-12-01 07:33:58 30 4
gpt4 key购买 nike

显然是在学习java。我能够让游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的东西,那么它默认为 Rock。我不需要循环。如果我放石头的话,游戏就完美了。如果我放置 RPS 以外的任何东西,它也可以完美工作并且默认为摇滚。但是,如果我选择“布”或“剪刀”,它会给出“布”和“剪刀”答案,并且默认为“石头”答案。关于为什么这不起作用的任何提示?

还有,我的方向是不是错了?我可以用更好的方式来做这件事吗?这感觉真的……充其量也不优雅。

    public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String userChoice="", userInput, compChoice="";
int ranInt = (int)(Math.random()*3);
if (ranInt == 0){
compChoice = "Rock";
} else if (ranInt == 1){
compChoice = "Paper";
}else if (ranInt == 2){
compChoice = "Scissors";
}
System.out.println("Please select one of [R/P/S]: ");
userInput = in.next();

if(userInput.equalsIgnoreCase("p")){
userChoice = "Paper ";
if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(userChoice + "beats "+compChoice +"- you win!");
}else if (compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println("A tie!");
}else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + userChoice + " - i win!");
}
}

if(userInput.equalsIgnoreCase("S")){
userChoice = "Scissors ";
if(compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: "+userChoice);
System.out.println("I choce: "+compChoice);
System.out.println(userChoice + "beats "+compChoice +"- you win!");
}else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println("A tie!");
}else if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + userChoice + " - i win!");
}
}
if(userInput.equalsIgnoreCase("R")){
userChoice = "Rock ";
if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println("A Tie!");
}else if(compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + userChoice + " - you lose!");
}else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(userChoice + " beats " + compChoice + " - you win!");
}
}
else {
userChoice = "";
System.out.println("Invalid selection, defaulting to rock.");
if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: Rock");
System.out.println("I chose: "+compChoice);
System.out.println("A Tie!");
}else if (compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: Rock");
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + "Rock - you lose!");
} else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: Rock");
System.out.println("I chose: "+compChoice);
System.out.println("Rock beats " + compChoice + " - you win!");
}
}
}

}

最佳答案

在伪代码中,代码的结构是:

if (paper) {
process paper
}

if (scissors) {
process scissors
}

if (rock) {
process rock
} else {
process default rock
}

请注意(因为这些情况是互斥的)如果 paperscissors 为 true,则 rock 为 false,并且 else 将执行。只需将结构更改为:

if (paper) {
process paper
} else if (scissors) {
process scissors
} else if (rock) {
process rock
} else {
process default rock
}

关于Java石头/剪刀/布,这样做错了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766160/

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