java in Java IF statements 中的错误输出代码用于一个简单的游戏
Wrong output code in java in Java IF statements for a simple game
我必须制作一个包含多个 类 和方法的剪刀石头布游戏。
类 之一是建立计算机玩家,它应该生成一个随机选择,然后看看它是否赢得了(didIWin
方法)玩家的选择。我测试了计算机的随机选择与玩家选择的石头、布和剪刀,这会告诉您您的方法是否有效。
我的输出给了我所有三个相同的答案,我不知道我做错了什么。我认为它必须在 didIWin
方法中。任何帮助将不胜感激。
这是我的代码:
public class Computer
{
//instance / member variables
private String choice;
private int choiceNumber;
public Computer()
{
//call random set Choice
randomSetChoice();
}
public String getChoice()
{
return "" + choice;
}
public void randomSetChoice()
{
//use Math.random()
int min = 1;
int max = 4;
choiceNumber = (int)(Math.floor(Math.random() * (max - min) + min));
//use switch case
switch(choiceNumber)
{
case 1: choice = "rock"; break;
case 2: choice = "paper"; break;
case 3: choice = "scissors"; break;
default: choice = "invalid input...try again";
}
}
/*
didIWin(Player p) will return the following values
0 - both players have the same choice
1 - the computer had the higher ranking choice
-1 - the player had the higher ranking choice
*/
public int didIWin(Player p)
{
String pc = p.getChoice();
if(choice == "rock")
{
if(pc == "rock")
{
return 0;
}
else if(pc == "paper")
{
return -1;
}
else
{
return 1;
}
}
else if(choice == "paper")
{
if(pc == "paper")
{
return 0;
}
else if(pc == "scissors")
{
return -1;
}
else
{
return 1;
}
}
else
{
if(pc == "scissors")
{
return 0;
}
else if(pc == "rock")
{
return -1;
}
else
{
return 1;
}
}
}
public String toString()
{
return "" + "Computer chose " + choice + "!";
}
}
实际上,如果您测试该方法 didWin()
,它会起作用!
我试过这个简单的例子:
public class Main {
static private String str = "scissors";
static private String choice = "paper";
static int didWin(){
String pc = str;
if(choice == "rock"){
if(pc == "rock"){
return 0;
}
else if(pc == "paper"){
return -1;
}
else{
return 1;
}
}
else if(choice == "paper"){
if(pc == "paper"){
return 0;
}
else if(pc == "scissors"){
return -1;
}
else{
return 1;
}
}
else {
if(pc == "scissors"){
return 0;
}
else if(pc == "rock"){
return -1;
}
else {
return 1;
}
}
}
public static void main(String[] args) {
System.out.println(didWin());
}
}
所有的尝试都成功了。
请注意,您在问题中写的是 didIWin()
而不是 didWin()
,所以 这可能就是问题所在。
也许您可以使用 Player
class 和您的一些尝试进行编辑。
我必须制作一个包含多个 类 和方法的剪刀石头布游戏。
类 之一是建立计算机玩家,它应该生成一个随机选择,然后看看它是否赢得了(didIWin
方法)玩家的选择。我测试了计算机的随机选择与玩家选择的石头、布和剪刀,这会告诉您您的方法是否有效。
我的输出给了我所有三个相同的答案,我不知道我做错了什么。我认为它必须在 didIWin
方法中。任何帮助将不胜感激。
这是我的代码:
public class Computer
{
//instance / member variables
private String choice;
private int choiceNumber;
public Computer()
{
//call random set Choice
randomSetChoice();
}
public String getChoice()
{
return "" + choice;
}
public void randomSetChoice()
{
//use Math.random()
int min = 1;
int max = 4;
choiceNumber = (int)(Math.floor(Math.random() * (max - min) + min));
//use switch case
switch(choiceNumber)
{
case 1: choice = "rock"; break;
case 2: choice = "paper"; break;
case 3: choice = "scissors"; break;
default: choice = "invalid input...try again";
}
}
/*
didIWin(Player p) will return the following values
0 - both players have the same choice
1 - the computer had the higher ranking choice
-1 - the player had the higher ranking choice
*/
public int didIWin(Player p)
{
String pc = p.getChoice();
if(choice == "rock")
{
if(pc == "rock")
{
return 0;
}
else if(pc == "paper")
{
return -1;
}
else
{
return 1;
}
}
else if(choice == "paper")
{
if(pc == "paper")
{
return 0;
}
else if(pc == "scissors")
{
return -1;
}
else
{
return 1;
}
}
else
{
if(pc == "scissors")
{
return 0;
}
else if(pc == "rock")
{
return -1;
}
else
{
return 1;
}
}
}
public String toString()
{
return "" + "Computer chose " + choice + "!";
}
}
实际上,如果您测试该方法 didWin()
,它会起作用!
我试过这个简单的例子:
public class Main {
static private String str = "scissors";
static private String choice = "paper";
static int didWin(){
String pc = str;
if(choice == "rock"){
if(pc == "rock"){
return 0;
}
else if(pc == "paper"){
return -1;
}
else{
return 1;
}
}
else if(choice == "paper"){
if(pc == "paper"){
return 0;
}
else if(pc == "scissors"){
return -1;
}
else{
return 1;
}
}
else {
if(pc == "scissors"){
return 0;
}
else if(pc == "rock"){
return -1;
}
else {
return 1;
}
}
}
public static void main(String[] args) {
System.out.println(didWin());
}
}
所有的尝试都成功了。
请注意,您在问题中写的是 didIWin()
而不是 didWin()
,所以 这可能就是问题所在。
也许您可以使用 Player
class 和您的一些尝试进行编辑。