为课堂做一个项目,我试图用与教授最终做的不同的方式去做。编译时我得到:
RPSLS.java:65: error: incompatible types: Object cannot be converted to int
switch (userChoice){
^
1 error
import javax.swing.JOptionPane;
import java.util.Random;
public class RPSLS {
public static void main(String[] args) {
//Setting choices and compChoice (for the number the computer chooses)
int compChoice;
final int Rock = 0;
final int Paper = 0;
final int Scissors = 0;
final int Lizard = 0;
final int Spock = 0;
//Comp Choice
Random generator = new Random();
compChoice = generator.nextInt(5) + 1;
//User Choice
Object[] possibleValues = {
"Rock", "Paper", "Scissors", "Lizard", "Spock"
};
Object userChoice = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]); //Set the default option (AFIAK it doesn't matter for this kind of thing)
System.out.println("The computer chose: " + compChoice);
System.out.println("The computer chose: " + userChoice);
//Set the userChoice equal to a number based on what was picked in userChoice question after both have entered choices and the choices are printed
//Values are to be set as follows:
//Rock 0
//Paper 1
//Scissors 2
//Lizards 3
//Spock 4
if (userChoice == "Rock") {
userChoice = String.valueOf(Rock);
} else if (userChoice == "Paper") {
userChoice = String.valueOf(Paper);
} else if (userChoice == "Scissors") {
userChoice = String.valueOf(Scissors);
} else if (userChoice == "Lizard") {
userChoice = String.valueOf(Lizard);
} else if (userChoice == "Spock") {
userChoice = String.valueOf(Spock);
}
//Determine who wins
switch (userChoice) {
case 0: //User chooses Rock
if (compChoice == 0) //Rock
{
System.out.println("Tie, try again.");
} else if (compChoice == 1) //Paper
{
System.out.println("Computer Win");
} else if (compChoice == 2) //Scissors
{
System.out.println("Player Win");
} else if (compChoice == 3) //Lizard
{
System.out.println("Player Win");
} else if (compChoice == 4) //Spock
{
System.out.println("Computer Win");
}
break;
case 1: //User chooses paper
if (compChoice == 0) //Rock
{
System.out.println("Player Win");
} else if (compChoice == 1) //Paper
{
System.out.println("Tie, try again.");
} else if (compChoice == 2) //Scissors
{
System.out.println("Computer Win");
} else if (compChoice == 3) //Lizard
{
System.out.println("Computer Win");
} else if (compChoice == 4) //Spock
{
System.out.println("Player Win");
}
break;
case 2: //User chooses scissors
if (compChoice == 0) //Rock
{
System.out.println("Computer Win");
} else if (compChoice == 1) //Paper
{
System.out.println("Player Win");
} else if (compChoice == 2) //Scissors
{
System.out.println("Tie, try again.");
} else if (compChoice == 3) //Lizard
{
System.out.println("Player Win");
} else if (compChoice == 4) //Spock
{
System.out.println("Computer Win");
}
break;
case 3: //User chooses lizard
if (compChoice == 0) //Rock
{
System.out.println("Computer Win");
} else if (compChoice == 1) //Paper
{
System.out.println("Player Win");
} else if (compChoice == 2) //Scissors
{
System.out.println("Computer Win");
} else if (compChoice == 3) //Lizard
{
System.out.println("Tie, try again.");
} else if (compChoice == 4) //Spock
{
System.out.println("Player Win");
}
break;
case 4: //User chooses spock
if (compChoice == 0) //Rock
{
System.out.println("Player Win");
} else if (compChoice == 1) //Paper
{
System.out.println("Computer Win");
} else if (compChoice == 2) //Scissors
{
System.out.println("Player Win");
} else if (compChoice == 3) //Lizard
{
System.out.println("Computer Win");
} else if (compChoice == 4) //Spock
{
System.out.println("Tie, try again.");
}
break;
default:
System.out.println("There was an error. Please try again.");
break;
}
}
}
最初我认为我可以根据所选择的选项将字符串设置为一个值。看来我不能,而且我不确定它希望我如何将开关设置为 int。我认为我可能能够做的是,在我最初的 if 语句中,我没有将字符串设置为等于一个值,而是根据用户在 joption 部分中的选择将其设置为一个值。任何帮助或建议将不胜感激。