为课堂做一个项目,我试图以与教授最终做的不同的方式做到这一点。编译时我得到:
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部分中用户的选择将int设置为一个值。任何帮助或建议将不胜感激。
答案 0 :(得分:0)
Java是strongly-typed programming language,这意味着您无法仅因为您想要隐式地将任何内容转换为int
。您必须通过(int)userChoice
或类似new Integer(userChoice)
或甚至userChoice.toInt()
(如果提供)等方式适当地转换它。因此...
switch((int)userChoice) { // Or whatever is the correct way for your objects to be converted to integers
...
}
预计会解决您的问题。无论如何,你没有给我们足够的信息来找出为你的特定对象进行转换的正确和有效方法。
答案 1 :(得分:-1)
首先,您不能在switch语句中使用Object
。正如它告诉你的那样,你需要一个int
。在这里给出这些不同的值(像这样,并使用小写):
final int rock = 0;
final int paper = 1;
final int scissors = 2;
final int lizard = 3;
final int spock = 4;
因此,创建另一个名为userChoiceNumber
的变量,并更改此位(看起来像这样,我还建议将用户输入转换为小写,因为这是一个赋值,我'让你弄清楚):
int userChoiceNumber = -1; // As a default value that you can check, or change to 0 for a valid default
if (userChoice.equals("Rock")) { // Instead of using == use equals
userChoiceNumber = rock;
} else if (userChoice.equals("Paper")) {
userChoiceNumber = paper;
} else if (userChoice.equals("Scissors")) {
userChoiceNumber = scissors;
} else if (userChoice.equals("Lizard")) {
userChoiceNumber = lizard;
} else if (userChoice.equals("Spock")) {
userChoiceNumber = spock;
}
然后在您的switch
语句中,您切换到使用userChoiceNumber
,并可以用final int
替换0-4:
//Determine who wins
switch (userChoiceNumber) {
case rock: //User chooses Rock
修改:从使用==
更改为使用.equals()
,正确处理字符串的比较。