从Enum Java调用随机值

时间:2017-03-14 04:43:32

标签: java enums

我有一个Enum类,一个Player类,以及一个名为Lisa的类,它扩展了Player类。我试图从Enum随机生成一个值(PAPER,ROCK或SCISSORS)。错误:“Roshambo的原始类型int没有字段ROCK。”任何建议或指示将不胜感激。这可能很明显,但这是我的拳头Java类和谷歌和Stackoverflow搜索没有帮助。这是我到目前为止编码的内容:

更新:感谢您的所有帮助。我在下面更新了我的整个程序。我想知道是否有人可以建议最好的方式/地点来实现逻辑来确定游戏的赢家/输家?这是完整的代码:

MAIN

package gameOfRoshambo;
import java.util.Scanner;
public class RoshamboApp {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);

    System.out.println("Welcome to Roshambo!");
    System.out.println("Enter your name:");

    //Create a new payer
    Player1 player1 = new Player1();
    String name = sc.nextLine();
    player1.setName(name);

    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {
        System.out.println("Hello " + name + ". " + "Would you like to play against Bart or Lisa? (B/L)");
        String opponent = sc.next();

            if(opponent.equalsIgnoreCase("B")){
                //Create a new Bart opponent
                Bart bart = new Bart();
                System.out.println(player1.getName() + ": " + player1.getChoice());
                System.out.println("Bart: " + bart.getRoshambo());

            }
            else if (opponent.equalsIgnoreCase("L")){
                //Create a new Lisa opponent
                Lisa lisa = new Lisa();
                System.out.println(player1.getName() + ": " + player1.getChoice());
                System.out.println("Lisa: " + lisa.getRoshambo());  

            }

        // Ask user if they want to continue
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }

    //Close Scanner
    System.out.println("Thanks for playing! Goodbye!");
    sc.close();
}

}

ENUM

package gameOfRoshambo;
public enum Roshambo

{ROCK, PAPER, SCISSORS;

public String toString() {
    switch(this) {
      case ROCK: return "Rock";
      case PAPER: return "Paper";
      case SCISSORS: return "Scissors";
      default: throw new IllegalArgumentException();
    }
  }
}

PLAYER

package gameOfRoshambo;
abstract class Player {
String name;
Roshambo roshambo;

abstract int generateRoshambo();

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Roshambo getRoshambo() {
    return roshambo;
}
public void setRoshambo(Roshambo newRoshambo) {
    roshambo = newRoshambo;
}
}

PLAYER1

package gameOfRoshambo;
import java.util.Scanner;

public class Player1 extends Player{

String player1 = "";

public Player1(){
    super();
    }

Scanner scan = new Scanner(System.in);

public Roshambo getChoice(){
    System.out.println("Enter Choice: Paper, Rock, Scissors (r/p/s): ");
    char playerChoice = scan.nextLine().toUpperCase().charAt(0);

    switch (playerChoice){
        case 'R':
            return Roshambo.ROCK;
        case 'P':
            return Roshambo.PAPER;
        case 'S':
            return Roshambo.SCISSORS;
        }
    System.out.println("Invalid input!");
return getChoice();
}

public String getPlayer1() {
    return player1;
}

public void setPlayer1(String player1) {
    this.player1 = player1;
}

@Override
int generateRoshambo() {
    // TODO Auto-generated method stub
    return 0;
}
}

BART

package gameOfRoshambo;
public class Bart extends Player {

public Bart(){
    super();
}

public Roshambo getRoshambo(){
    return Roshambo.ROCK;
    }

@Override
int generateRoshambo() {
    // TODO Auto-generated method stub
    return 0;
}
}

LISA

package gameOfRoshambo;
import java.util.Random;
public class Lisa extends Player {
private Random rand;

public Lisa(){
    super();
    rand = new Random();
}

public Roshambo getRoshambo(){
    int shoot = rand.nextInt(3);
    return Roshambo.values()[shoot];
    }

@Override
int generateRoshambo() {
    return 0;
}
}

2 个答案:

答案 0 :(得分:1)

  • 您应将roshambo字段存储为Roshambo而不是int,并相应地更新您的setter和getter。这是因为在Java中,枚举无法转换为int。有关说明,请参阅下面的堆栈溢出链接:
  

Cast Int to enum in Java

  • 字段名称应以小写字母开头
  • 使用Roshambo.values()[choice]
  • 摆脱1 +中的1 + rand.nextInt(3);,因为nextInt()方法的第一个枚举值位于第0位。所以
Roshambo.values()[0] = ROCK
Roshambo.values()[1] = PAPER
Roshambo.values()[2] = SCISSORS
  • 在Lisa构造函数中,更改为rand = new Random()而不是Random rand = new Random(),以避免分配给构造函数完成后丢失的新局部变量

请参阅我在下面为您提供的代码段

玩家类

package gameOfRoshambo;

abstract class Player {
    String name;
    Roshambo roshambo;

    abstract int generateRoshambo();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Roshambo getRoshambo() {
        return roshambo;
    }
    public void setRoshambo(Roshambo newRoshambo) {
        roshambo = newRoshambo;
    }
}

Lisa Class

package gameOfRoshambo;

import java.util.Random;

public class Lisa extends Player {

    private Random rand;

    public Lisa(){
        super();
        rand = new Random();
    }

    public Roshambo getRoshambo(){
        int choice = rand.nextInt(3);
        return Roshambo.values()[choice];
        }

    @Override
    int generateRoshambo() {
        return 0;
    }

}

使用上述新实现时,您也不必使用abstract int generateRoshambo()方法,因此请考虑将其删除并在Lisa中实现...

答案 1 :(得分:0)

您的字段Roshambo的类型为int。我想你想把它宣布为更像这样的东西:

Roshambo roshambo;

将字段名称大写是不好的做法。在这种情况下,它使您感到困惑,因为您将字段名称与类型混合在一起。您必须在代码中的其他几个位置将int替换为Roshambo