需要使用枚举序数来创建一个计数器。

huangapple 未分类评论47阅读模式
英文:

Need to make a Counter using enum ordinals

问题

以下是您提供的代码的翻译部分:

我需要为剪刀石头布游戏制作一个计数器数组但是不知道如何使用户输入调用枚举我需要使游戏的动作成为一个计数器以便进行使用和比较

这是我的枚举

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;
	}


这是会调用它的我的类

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();
				}
			}			
			
		}

请注意,这只是代码的翻译部分,我没有回答关于如何解决您提出的问题的具体问题。如果您需要有关代码的具体问题的帮助,请提出具体的问题,我将尽力提供帮助。

英文:

I need to make a counter array for a Rock Paper Scissors game but can not figure out how to make user input call the enum, and i need to make the moves for the game a counter for them to be used and compared to.

This is my enum

public enum Moves {

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


private int countOf = 0;
private int moveVal;
private ArrayList&lt;Moves&gt; movesList = new ArrayList&lt;&gt;();


Moves(int moveVal) {
	
	
}

public int getmoveVal() {
	return moveVal;
}

public int getCountOf() {
	
for(Moves moveList : Moves.values()) {
	countOf++;
	
}
	return countOf;
}

And this is my class that would call it

public static void main(String[] args) {

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

int playerMove = 0;
for(int i = 0; i &lt; move.length; i++) {
	
}
	
	boolean continueGame = true;
	
	@SuppressWarnings(&quot;resource&quot;)
	Scanner keyboard = new Scanner(System.in);
	while(continueGame)
	{
		System.out.println(rsg.moveChoices());
		
		System.out.println(&quot;Enter Move:(1,2,3,4 or 5):&quot;);
		playerMove = keyboard.nextInt();
		rsg.playRound(move);
		System.out.printf(&quot;AI %s!%n&quot;, rsg.getAIOutcome().toString());
		System.out.printf(&quot;Player %s!%n&quot;, rsg.getPlayerOutcome().toString());
		
		System.out.println(rsg.moveOutcome());
		System.out.println(rsg.currentScore());
		
		
		if(rsg.isGameOver())
		{
			System.out.println(rsg.currentWinTotal());
			
			System.out.println(&quot;Do you want to Play Again(1 - Yes , 2 - No):&quot;);
			int answer = keyboard.nextInt();
			
			
			if(answer == 2)
			{
				continueGame = false; 
			}
			else
			{
				rsg.reset();
			}
		}			
		
	}

答案1

得分: 0

你可以通过迭代values来找到与moveVal匹配的Move

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

You can iterate through values to find a Move matching that moveVal:

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

huangapple
  • 本文由 发表于 2020年5月5日 05:05:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61601593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定