作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个枚举类、一个玩家类和一个名为 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
而不是 int
并相应地更新您的 setter 和 getter。这是因为在 Java 中,枚举无法转换为 int
。请参阅下面的堆栈溢出链接以获取说明:Roshambo.values()[选择]
1 + rand.nextInt(3);
中的 1 +
,因为 nextInt()
方法具有第一个枚举位置 0 的值。所以 Roshambo.values()[0] = ROCK
Roshambo.values()[1] = PAPER
Roshambo.values()[2] = SCISSORS
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/
我是一名优秀的程序员,十分优秀!