gpt4 book ai didi

java - 从 Java 枚举调用随机值

转载 作者:行者123 更新时间:2023-12-02 13:40:09 24 4
gpt4 key购买 nike

我有一个枚举类、一个玩家类和一个名为 Lisa 的类,该类扩展了玩家类。我试图从枚举中随机生成一个值(PAPER、ROCK 或 SCISSORS)。错误:“Roshambo 的原始类型 int 没有 ROCK 字段。”任何建议或指示将不胜感激。这可能是显而易见的,但这是我的拳头 Java 类(class),Google 和 Stackoverflow 搜索没有帮助。这是我到目前为止编写的代码:

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

主要内容

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

}

枚举

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

玩家

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

玩家1

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

捷运

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

丽莎

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

最佳答案

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

Cast Int to enum in Java

  • 字段名称应以小写字母开头
  • 使用Roshambo.values()[选择]
  • 去掉 1 + rand.nextInt(3); 中的 1 +,因为 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;
}
}

丽莎类

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 intgenerateRoshambo() 方法,因此请考虑删除它及其在 Lisa 中的实现...

关于java - 从 Java 枚举调用随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777890/

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