编辑:我修复了我的程序以便它可以工作,我添加了括号。 但是,我遇到了另一个错误。当用户赢得游戏时,我的代码不会结束。我希望它结束,所以请帮助我。以下是我的代码:
public void rockPaperScissors ()
{
System.out.println ("First one to 3 Points wins the battle!");
char again = 'y';
while (again == 'y'){
int rPoint = 0;
int uPoint = 0;
int game = 1;
System.out.println ("\nLET THE BATTLE BEGIN!");
while (uPoint < 3 || rPoint < 3) {
String user = uChoice ();
System.out.println (user + ", I choose you!");
String rival = rChoice ();
System.out.println ("Your rival has chosen " + rival + "!");
String win = winner (rival, user);
if (win == "r")
{
rPoint++;
System.out.println ("\nYour rival has won the match.");
}
else if (win == "u")
{
uPoint++;
System.out.println ("\nYou have won the match!");
}
else
System.out.println ("\nBoth Pokemon have fainted! It's a tie!");
System.out.println ("Points: You " + uPoint + " Rival: " + rPoint);
if (uPoint == 3) {
System.out.println ("Congrats! You have beaten your rival!");
break;
}
else if (rPoint == 3) {
System.out.println ("Too bad! Your rival has beaten you!");
again = IBIO.inputChar ("\nWant to try again? (y/n) ");
uPoint = 0;
rPoint = 0;
}
}
}
}
public boolean isValid (String c)
{
if (c.equals ("Bulbasaur") || c.equals ("BULBASAUR") || c.equals ("bulbasaur") || c.equals ("B") || c.equals ("b") || c.equals ("Charmander") || c.equals ("CHARMANDER") || c.equals ("charmander") || c.equals ("c") || c.equals ("C") || c.equals ("CHARMANDER") || c.equals ("SQUIRTLE") || c.equals ("Squirtle") || c.equals ("squirtle") || c.equals ("s") || c.equals ("S"))
return true;
else
return false;
}
public String uChoice ()
{
printSlow ("Pokemon: Bulbasaur, Charmander, Squirtle");
String c = IBIO.inputString ("Which Pokemon do you choose? ");
while (!isValid (c))
{
System.out.println ("You don't have that Pokemon. Try again.");
c = IBIO.inputString ("Which Pokemon do you choose? ");
}
if (c.equals ("Bulbasaur") || c.equals ("BULBASAUR") || c.equals ("bulbasaur") || c.equals ("b") || c.equals ("B"))
return "Bulbasaur";
else if (c.equals ("Charmander") || c.equals ("CHARMANDER") || c.equals ("charmander") || c.equals ("c") || c.equals ("C"))
return "Charmander";
else
return "Squirtle";
}
public String rChoice ()
{
int num = (int)(Math.random ()*3) + 1;
if (num == 1)
return "Bulbasaur";
else if (num == 2)
return "Charmander";
else
return "Squirtle";
}
public String winner (String rival, String user)
{
if ((rival == "Bulbasaur" && user == "Squirtle") || (rival == "Charmander" && user == "Bulbasaur") || (rival == "Squirtle" && user == "Charmander"))
return "r";
else if ((user == "Bulbasaur" && rival == "Squirtle") || (user == "Charmander" && rival == "Bulbasaur") || (user == "Squirtle" && rival == "Charmander"))
return "u";
else
return "b";
}
答案 0 :(得分:2)
public void rockPaperScissors ()
{...
while (again == 'y')
{
...
}
//HERE
public boolean isValid (String c)
{...}
我在}
评论中遗漏了//
。