Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法错误“;找不到符号“:只有一种方法出现此错误(finalResults)_Java - Fatal编程技术网

Java 方法错误“;找不到符号“:只有一种方法出现此错误(finalResults)

Java 方法错误“;找不到符号“:只有一种方法出现此错误(finalResults),java,Java,所以,这个程序是为了玩一个像石头,布,剪刀这样的游戏。它被称为猴子、机器人、海盗、忍者、僵尸。我调用的每个方法都有效,除了“finalResults”方法。我调用finalResults就像调用其他方法一样,但是当我调用finalResults时,括号中的变量带红色下划线,并给出错误“找不到符号”。所有这些方法都在同一个类中,所以我不知道我做错了什么。谢谢大家 package project4; import java.util.Scanner; /** * * @au

所以,这个程序是为了玩一个像石头,布,剪刀这样的游戏。它被称为猴子、机器人、海盗、忍者、僵尸。我调用的每个方法都有效,除了“finalResults”方法。我调用finalResults就像调用其他方法一样,但是当我调用finalResults时,括号中的变量带红色下划线,并给出错误“找不到符号”。所有这些方法都在同一个类中,所以我不知道我做错了什么。谢谢大家

    package project4;

    import java.util.Scanner;


/**
 *
 * @author seanchalant
 */
public class Project4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
         Scanner in = new Scanner(System.in);//Scanner in
            for (int Round = 1; Round<=5; Round++)//for loop for the 5 rounds
            {     
                System.out.println("Round# "+Round +"\n");//print statement for number of round using 'for loop'
                System.out.println("Please enter a number from 1-5:");
                int PlayerChoice = in.nextInt();
                int ComputerChoice = (int)(Math.random()*5) + 1;
                while (ComputerChoice==PlayerChoice)
                {
                    System.out.println("There is a tie! Please enter a new choice (1-5):");
                    PlayerChoice = in.nextInt();
                    ComputerChoice = (int)(Math.random()*5) +1;
                }
                convertChoice(PlayerChoice);
                determineWinner(PlayerChoice, ComputerChoice);

            }
      finalResults(PlayerWin, ComputerWin, user, computer); *ERROR IS HERE*
    }

    public enum PlayerChoice {Monkey, Robot, Pirate, Ninja, Zombie};
    public static PlayerChoice convertChoice(int PlayerChoice)
    {

                switch (PlayerChoice)
                {
                    case 1:
                        return Project4.PlayerChoice.Monkey;
                    case 2:
                        return Project4.PlayerChoice.Robot;
                    case 3:
                        return Project4.PlayerChoice.Pirate;
                    case 4:
                        return Project4.PlayerChoice.Ninja;
                    case 5:
                        return Project4.PlayerChoice.Zombie;    
                }
                return null;

    }
    public static PlayerChoice determineWinner(int PlayerChoice, int ComputerChoice)
    {       
        int round = 0;
        int PlayerWin = 0;
        int ComputerWin = 0;        
        if (PlayerChoice==1 && ComputerChoice==2)//if statements for the different scenarios. 
            {
                System.out.println("Monkey annoys robot enough to cause malfunction. Player wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==1 && ComputerChoice==3)
            {
                System.out.println ("Pirate shoots monkey in the face. Computer wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==1 && ComputerChoice==4)
            {
                System.out.println ("Monkey embraces it's inner ninja and wins? Player wins?");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==1 && ComputerChoice==5)
            {
                System.out.println ("Zombie uses monkey for sustenance. Computer wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==2 && ComputerChoice==1)
            {
                System.out.println ("Monkey annoys robot enough to cause malfunction. Computer Wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==2 && ComputerChoice ==3)
            {
                System.out.println ("Pirate pours rum down robot's throat. It breaks. Computer Wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==2 && ComputerChoice==4)
            {
                System.out.println ("Ninja gets decapitated by lasers. Player wins!");
                PlayerWin++; 
                round++;
            }
            if (PlayerChoice==2 && ComputerChoice==5)
            {
                System.out.println ("Zombie gets decapitated by lasers. Player wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==3 && ComputerChoice==1)
            {
                System.out.println ("Pirate shoots monkey in the face. Player wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==3 && ComputerChoice==2)
            {
                System.out.println ("Pirate pours rum down robot's throat. It breaks. Player Wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==3 && ComputerChoice==4)
            {
                System.out.println ("Ninja poisons pirate with blowdart. Computer wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==3 && ComputerChoice==5)
            {
                System.out.println ("Zombie uses pirate for sustenance. Computer wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==4 && ComputerChoice==1)
            {
                System.out.println ("Monkey embraces its inner ninja and wins? Computer wins?");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==4 && ComputerChoice==2)
            {
                System.out.println ("Ninja gets decapitated by lasers. Player wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==4 && ComputerChoice==3)
            {
                System.out.println ("Ninja poisons pirate with blowdart. Player wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==4 && ComputerChoice==5)
            {
                System.out.println ("Ninja laughs at slow zombie and decapitates it. Player Wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==5 && ComputerChoice==1)
            {
                System.out.println ("Zombie uses monkey for sustenance. Player wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==5 && ComputerChoice==2)
            {
                System.out.println ("Zombie gets decapitated by lasers. Computer wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerChoice==5 && ComputerChoice==3)
            {
                System.out.println ("Zombie uses pirate for sustenance. Player wins!");
                PlayerWin++;
                round++;
            }
            if (PlayerChoice==5 && ComputerChoice==4)
            {
                System.out.println ("Ninja laughs at slow zombie and decapitates it. Computer Wins!");
                ComputerWin++;
                round++;
            }
            if (PlayerWin==PlayerWin+1)
            {
                int[]user = new int [round];
            }
            if (ComputerWin==ComputerWin+1)
            {
                int[]computer = new int[round];
            }
          return null; 
    }
    public static PlayerChoice finalResults(int PlayerWin, int ComputerWin, int[] user, int[] computer)
    {
            System.out.println("Final Results:\n");
            if (PlayerWin==1)
            {
                System.out.println("The player won round "+user[1]+".\n");
            }
            if (PlayerWin==2)
            {
                System.out.println("The player won rounds "+user[1]+" and "+user[2]+".\n");
            }
            if (PlayerWin==3)
            {
                System.out.println("The player won rounds "+user[1]+", "+user[2]+", "+user[3]+".\n");
            }
            if (PlayerWin==4)
            {
                System.out.println("The player won rounds "+user[1]+", "+user[2]+", "+user[3]+", "+user[4]+".\n");

            }
            if (PlayerWin==5)
            {
                System.out.println("The player won rounds "+user[1]+", "+user[2]+", "+user[3]+", "+user[4]+", "+user[5]+".\n");
            }
            if (ComputerWin==1)
            {
                System.out.println("The computer won round "+computer[1]+".\n");
            }
            if (ComputerWin==2)
            {
                System.out.println("The computer won rounds "+computer[1]+" and "+computer[2]+".\n");
            }
            if (ComputerWin==3)
            {
                System.out.println("The computer won rounds "+computer[1]+", "+computer[2]+", "+computer[3]+".\n");
            }
            if (ComputerWin==4)
            {
                System.out.println("The computer won rounds "+computer[1]+", "+computer[2]+", "+computer[3]+", "+computer[4]+".\n");

            }
            if (ComputerWin==5)
            {
                System.out.println("The computer won rounds "+computer[1]+", "+computer[2]+", "+computer[3]+", "+computer[4]+", "+computer[5]+".\n");
            }
            System.out.println("You won a total of "+PlayerWin+"\n");
            System.out.println("The computer won a total of "+ComputerWin+"\n");
             if (PlayerWin>ComputerWin)
            {
                System.out.println("You win the game!\n");
            }
            if (ComputerWin>PlayerWin)
            {
                System.out.println("Computer wins the game!\n");
            }
        return null;
    }
}
package项目4;
导入java.util.Scanner;
/**
*
*@作者seanchalant
*/
公共类项目4{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
//此处的TODO代码应用程序逻辑
扫描仪输入=新扫描仪(System.in);//扫描仪输入
用于(整数轮=1;整数轮)
{
System.out.println(“你赢了游戏!\n”);
}
如果(ComputerWin>PlayerWin)
{
System.out.println(“计算机赢得游戏!\n”);
}
返回null;
}
}

在for循环之外创建变量,或/或给它们一个值:

int PlayerWin;
int ComputerWin;
int[] user;
int[] computer;

       for (int Round = 1; Round<=5; Round++)//for loop for the 5 rounds
        {     
            System.out.println("Round# "+Round +"\n");//print statement for number of round using 'for loop'
            System.out.println("Please enter a number from 1-5:");
            PlayerChoice = in.nextInt();
            ComputerChoice = (int)(Math.random()*5) + 1;
            while (ComputerChoice==PlayerChoice)
            {
                System.out.println("There is a tie! Please enter a new choice (1-5):");
                PlayerChoice = in.nextInt();
                ComputerChoice = (int)(Math.random()*5) +1;
            }
            convertChoice(PlayerChoice);
            determineWinner(PlayerChoice, ComputerChoice);

        }
finalResults(PlayerWin, ComputerWin, user, computer);
int-PlayerWin;
int ComputerWin;
int[]用户;
int[]计算机;

对于(int Round=1;Round,错误语句是明确的,这意味着您正在使用未定义的变量:

finalResults(PlayerWin, ComputerWin, user, computer);
在这里,您尝试使用不存在的变量。实际上,您正在尝试使用在其他方法中创建的、无法从
main
方法访问的变量

您应该考虑将这些变量声明为类成员,而不是在每个方法中声明,如果您想使用它们,并在所有方法中共享它们。

比如:

public class Project4 {
    private static int PlayerWin = 0;
    private static int ComputerWin = 0; 
    ...
    // methods
 }

在使用它们之前,您需要先创建它们。它们都已创建。PlayerWin、ComputerWin、[]用户和[]计算机是用determineWinner方法创建的,然后我在最后的结果中调用了PlayerWin和ComputerWinmethod@sam你是说它们是?如果你在determineWinner中创建它们,你不能在main方法中使用它们,因为它们超出了范围。谢谢@eush77@sam还有编辑按钮:)我很确定它们都已经被创造出来了。PlayerWin、ComputerWin、[]用户和[]计算机是在determineWinner方法中创建的。然后我在finalResults方法中调用了PlayerWin和ComputerWin。@Sean,是的,但它们是局部变量,只能在创建它们的范围内访问,因此它们不可访问。但是这些变量已经在for循环之外的另一个方法“determineWinner”中创建,然后在“最终结果”方法中调用。我非常困惑。谢谢你的回答,但是现在在将它们定义为自己的类成员之后,值不是从其他方法继承过来的,而是0。有什么想法吗?