似乎无法修复我的代码,每当我尝试输入值时,它都会显示为错误。此外,我使用ibio的东西来获取输入所以请在进行代码更正时忽略它并且顺便感谢。它会帮助我很多,因为我似乎无法弄清楚如何做到这一点......
编辑:我遇到了另一个问题,用户输入总是成为剪刀,尽管他们输入了什么。例如:
Options: Rock, Paper, Scissors
Your choice? Rock
You have selected s
public class rockPaperScissors
{
public static void main (String args[])
{
new rockPaperScissors ();
}
public rockPaperScissors ()
{
int cPoint = 0;
int uPoint = 0;
int game = 1;
char playAgain = 'y';
System.out.println ("-----Rock, Paper, Scissors------\n");
while (playAgain == 'y')
{
char user = userChoice ();
System.out.println ("You have selected " + user);
char comp = compChoice ();
System.out.println ("The computer has selected " + comp);
char win = winner (comp, user);
if (win == 'c')
{
cPoint++;
System.out.println ("\nThe computer wins!");
}
else if (win == 'u')
{
uPoint++;
System.out.println ("\nYou win!");
}
else
System.out.println ("\nThere is a tie!");
System.out.println ("Points: You: " + uPoint + " Computer: " + cPoint);
playAgain = IBIO.inputChar ("\nPlay again? (y/n) ");
System.out.println ("");
}
System.out.println ("Goodbye!");
}
public boolean isValid (String c)
{
/* All valid data:
Rock, ROCK, rock, r, R
Paper, PAPER, paper, p, P
Scissors, SCISSORS, scissors, s, S
return true if valid, false otherwise
*/
if (c=="Rock" || c=="ROCK" || c == "rock" || c=="r" || c=="R" || c=="Scissors" || c=="SCISSORS" || c=="scissors" || c=="s" || c=="S" || c=="Paper" || c=="PAPER" || c=="paper" || c=="p" || c=="P")
return true;
else
return false;
}
public char userChoice ()
{ //returns r, p or s, based on the user's choice
//print options: rock, paper, scissors
System.out.println ("Options: Rock, Paper, Scissors");
//ask for user's choice. will need to store in String
String c = IBIO.inputString ("Your choice? ");
//Loop: if invalid input, ask again
//stopping condition is the isValid method!!
//something like: while(!isValid(choice))
while (!isValid(c))
{
System.out.println ("That choice is invalid. Try another choice.");
c = IBIO.inputString ("Your choice? ");
}
//If: to standardize values
//if you've got one of Rock, ROCK, rock, r, R, then return 'r'.
if (c=="Rock" || c=="ROCK" || c=="rock" || c=="r" || c=="R")
return 'r';
//else if you've got one of Paper, PAPER, paper, p, P, then return 'p'.
else if (c=="Paper" || c=="PAPER" || c=="paper" || c=="p" || c=="P")
return 'p';
//else return 's';
else
return 's';
}
public char compChoice ()
{ //make a random number between 1 and 3
//if the number is 1, return r; 2 return s; 3 return p
int num = (int)(Math.random () * 3) + 1;
if (num == 1)
return 'r';
else if (num == 2)
return 's';
else
return 'p';
}
public char winner (char comp, char user)
{ //comp and user both hold one of r, s, p
//returns c for computer, u for user and b for both
if ((comp == 'r' && user == 's') || (comp == 's' && user == 'r') || (comp == 'p' && user == 's'))
return 'c';
if ((user == 'r' && comp == 's') || (user == 's' && comp == 'r') || (user == 'p' && comp == 's'))
return 'u';
else
return 'b';
}
}
答案 0 :(得分:1)
您的问题很明显,每次使用==
比较字符串时,它都会返回false。我告诉你要查看副本以解决这个问题,但我强烈建议用一些方法将代码分成更小的部分。
public boolean isRock(String input) {
String lower = input.toLowerCase();
return lower.equals("r") || lower.equals("rock");
}
public boolean isPaper(String input) {
String lower = input.toLowerCase();
return lower.equals("p") || lower.equals("paper");
}
public boolean isScissors(String input) {
String lower = input.toLowerCase();
return lower.equals("s") || lower.equals("scissors");
}
public boolean isValid(String input) {
return isRock(input) || isPaper(input) || isScissors(input);
}
这会将您的其他条件简化为
if (isRock(c))
return 'r';
else if (isPaper(c))
return 'p';
else
return 's';