gpt4 book ai didi

java - 为什么我的程序没有显示正确的输出?

转载 作者:行者123 更新时间:2023-12-01 13:06:03 24 4
gpt4 key购买 nike

首先,我对编程还是个新手。我的任务是制作一个没有数组的石头剪刀布程序。根据我们之前的选择,程序会选择相应的武器。在程序结束时,如果用户不想继续,我们会显示他们的总体分数。所有这些都应该在我们在 main 内部调用的另一个方法中。

正如我之前所说,我对编程非常陌生,因此除了数组之外,if else 语句是我知道如何执行此操作的唯一方法(还有很多方法)。

import java.util.Scanner;

public class ASSIGNMENT
{
public static void main (String [] args)
{
game();

}//end main

public static void game ()
{
Scanner kb = new Scanner(System.in);

//assigning variables
int rock = 1;
int scissors = 2;
int paper = 3;
int weaponAI = 0;
int weaponP = 0;
int wins = 0;
int losses = 0;
int ties = 0;
int rockNum = 0;
int scissorsNum = 0;
int paperNum = 0;
String yorn;

do
{
//generate random numbe to decide weapon
double weaponNum = (Math.random() * 3)+ 1;
System.out.println(weaponNum);
if (weaponNum >= 1 && weaponNum < 2)
{
if (rockNum > scissorsNum && rockNum > paperNum)
weaponAI = paper;
else if (paperNum > scissorsNum && paperNum > rockNum)
weaponAI = scissors;
else
weaponAI = rock;
}
else if (weaponNum >= 2 && weaponNum < 3)
{
if (scissorsNum > rockNum && scissorsNum > paperNum)
weaponAI = rock;
else if (paperNum > scissorsNum && paperNum > rockNum)
weaponAI = scissors;
else
weaponAI = paper;
}
else if (weaponNum >= 3)
{
if (scissorsNum > rockNum && scissorsNum > paperNum)
weaponAI = rock;
else if (rockNum > scissorsNum && rockNum > paperNum)
weaponAI = paper;
else
weaponAI = scissors;
}
//prompting for guess and assigning
System.out.println("BATTLE START!");
System.out.println("Choose your weapon: ");
String weaponStr = kb.nextLine();
if (weaponStr == "Rock" || weaponStr == "rock")
{
weaponP = rock;
++rockNum;
}
else if (weaponStr == "Scissors" || weaponStr == "scissors")
{
weaponP = scissors;
++scissorsNum;
}
else if (weaponStr == "Paper" || weaponStr == "paper")
{
weaponP = paper;
++paperNum;
}
else if (weaponStr =="quit" || weaponStr == "Quit")
System.exit(-1);

//comparing AI and Players weapon
//tied
if (weaponAI == rock && weaponP == rock)
{
System.out.println("You both chose rock");
++ties;
}
else if (weaponAI == paper && weaponP == paper)
{
System.out.println("You both chose paper");
++ties;
}
else if (weaponAI == scissors && weaponP == scissors)
{
++ties;
System.out.println("You both chose scissors");
}
//AI wins
else if (weaponAI == rock && weaponP == scissors)
{
++losses;
System.out.println("AI wins");
}
else if (weaponAI == paper && weaponP == rock)
{
++losses;
System.out.println("AI wins");
}
else if (weaponAI == scissors && weaponP == paper)
{
++losses;
System.out.println("AI wins");
}
//Player wins
else if (weaponAI == scissors && weaponP == rock)
{
++wins;
System.out.println("You win!");
}
else if (weaponAI == rock && weaponP == paper)
{
++wins;
System.out.println("You win!");
}
else if (weaponAI == paper && weaponP == scissors)
{
++wins;
System.out.println("You win!");
}
//prompting user to play again
System.out.println("Would you like to play again? Y or N?");
yorn = kb.nextLine();

}//end do
while (yorn == "y" || yorn == "Y" || yorn == "Yes" || yorn == "yes");

//Displaying scores
System.out.println("Game over.");
System.out.println("You had " + wins + " wins.");
System.out.println("You had " + losses + " losses.");
System.out.println("You had " + ties + " ties.");
System.out.println("You chose rock " + rockNum + " times.");
System.out.println("You chose paper " + paperNum + " times.");
System.out.println("You chose scissors " + scissorsNum + " times.");

}//end game
}//end class

我不确定为什么当我输入“rock”和“y”时我的输出如下

1.5102368482522261

BATTLE START!

Choose your weapon:

rock

Would you like to play again? Y or N?

y

Game over.

You had 0 wins.

You had 0 losses.

You had 0 ties.

You chose rock 0 times.

You chose paper 0 times.

You chose scissors 0 times.

这几乎就像程序忽略了我所有的 if else 语句。我确信这是一些我不明白的简单事情......

最佳答案

为了比较 String 内容,您必须将 == 替换为 equalsIgnoreCase()

您不能使用 == 运算符比较两个字符串内容。 java中的==运算符比较字符串引用(如果一个对象与另一个对象相同)

为了让你的代码正常工作,你需要这样的东西:

if (weaponStr.equalsIgnoreCase("Rock")){
...
}

要了解 Java 中如何进行字符串比较,您还可以查看:

关于java - 为什么我的程序没有显示正确的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23260786/

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