java - 需要使用枚举序数创建一个计数器

标签 java arrays enums

我需要为石头剪刀布游戏制作一个计数器数组,但无法弄清楚如何让用户输入调用枚举,并且我需要将游戏的 Action 作为计数器,供它们使用和比较.

这是我的枚举

public enum Moves {

Rock(1), 
Paper(2), 
Scissors(3), 
Lizard(4), 
Spock(5), ;


private int countOf = 0;
private int moveVal;
private ArrayList<Moves> movesList = new ArrayList<>();


Moves(int moveVal) {


}

public int getmoveVal() {
    return moveVal;
}

public int getCountOf() {

for(Moves moveList : Moves.values()) {
    countOf++;

}
    return countOf;
}

这是我的类(class),会调用它

public static void main(String[] args) {

RockPaperScissorGame rsg = new RockPaperScissorGame(3); 
Moves move[] = Moves.values();

int playerMove = 0;
for(int i = 0; i < move.length; i++) {

}

    boolean continueGame = true;

    @SuppressWarnings("resource")
    Scanner keyboard = new Scanner(System.in);
    while(continueGame)
    {
        System.out.println(rsg.moveChoices());

        System.out.println("Enter Move:(1,2,3,4 or 5):");
        playerMove = keyboard.nextInt();
        rsg.playRound(move);
        System.out.printf("AI %s!%n", rsg.getAIOutcome().toString());
        System.out.printf("Player %s!%n", rsg.getPlayerOutcome().toString());

        System.out.println(rsg.moveOutcome());
        System.out.println(rsg.currentScore());


        if(rsg.isGameOver())
        {
            System.out.println(rsg.currentWinTotal());

            System.out.println("Do you want to Play Again(1 - Yes , 2 - No):");
            int answer = keyboard.nextInt();


            if(answer == 2)
            {
                continueGame = false; 
            }
            else
            {
                rsg.reset();
            }
        }           

    }

最佳答案

您可以遍历values来查找与moveVal匹配的Move:

public Move fromMoveVal(int moveVal) {
  for (Move m : Move.values()) if (m.moveVal == moveVal) return m;
  return null;
}

关于java - 需要使用枚举序数创建一个计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61601593/

相关文章:

c - 对二叉树进行排序 - 函数达到 seg。过错

php - 我得到的 API 响应是什么类型的结果?

c++ - C++ 枚举类型可以作为函数调用吗?或者它只是一种不同风格的转换?

javascript - 将键值对添加到数组中的对象 - Javascript

c - 当 -1L、1U 和 -1UL 是符号常量时,-1L < 1U 和 -1L > UL 的输出与声明为枚举常量时不同

python - Cython 中的重复枚举成员名称 - 重新声明错误?

java - Eclipse .classpath 位置

java - 如果缺少 @PreAuthorize 注释,则 Spring Security : Deny access to controller methods,

java - 如何使用java向该程序添加总票数,数据库是ms sql

java - 为什么这个通用方法不安全?