所以,这个程序是为了玩像摇滚,纸张,剪刀这样的游戏。它被称为猴子,机器人,海盗,忍者,僵尸。除了方法“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;
}
}
答案 0 :(得分:0)
在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);
答案 1 :(得分:0)
错误陈述是明确的,这意味着您正在使用未定义的变量:
finalResults(PlayerWin, ComputerWin, user, computer);
在这里,您尝试使用不存在的变量。实际上,您正在尝试使用在其他方法中创建但无法从main
方法访问的变量。
如果要使用它们并在所有方法中共享它们,您应该考虑将这些变量声明为类成员而不是每种方法。
喜欢:
public class Project4 {
private static int PlayerWin = 0;
private static int ComputerWin = 0;
...
// methods
}