开始我还是新手编程。我的任务是制作一个没有阵列的石头剪刀程序。根据我们以前的选择,程序将选择相应的武器。在程序结束时,如果用户不想继续,我们将显示他们的总分。所有这些都应该是我们在main中调用的另一种方法。
正如我之前所说,我对编程非常陌生,所以在数组之外,if else语句是我知道如何做到这一点的唯一方法(以及很多em)。
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
我不确定为什么输入&#34; rock&#34;和&#34; y&#34;
1.5102368482522261
BATTLE START!
选择你的武器:
岩石
你想再玩一次吗?是或否?ý
游戏结束。
你有0胜。
你有0输。
你有0个关系。
你选择摇滚0次。
您选择纸张0次。
你选择剪刀0次。
这几乎就像程序忽略了我的所有if else语句。我确信这很简单,我不理解......
答案 0 :(得分:1)
为了比较String
内容,您必须将==
替换为equalsIgnoreCase()
。
您无法将两个字符串内容与==
运算符进行比较。 java中的==
运算符比较String references (如果一个Object是另一个Object)
要使代码正常工作,您需要以下内容:
if (weaponStr.equalsIgnoreCase("Rock")){
...
}
要了解如何在Java中完成String比较,您还可以看到: