gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:55:16 25 4
gpt4 key购买 nike

我需要为石头剪刀布游戏制作一个计数器数组,但无法弄清楚如何让用户输入调用枚举,并且我需要将游戏的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com