我对于如何保存从“ makeThrow()”和“ makeThrow(int throwType)”构造函数生成的整数,然后将它们传递给“ lastThrow()”方法感到困惑。
import java.util.Random;
public class RochambeauPlayer
extends java.lang.Object
{
private String name;
private int wins;
private int throwType;
public java.lang.String lastThrow(){
if(throwType == 0){
String rock = "ROCK";
return rock;
}
else if(throwType == 1){
String paper = "PAPER";
return paper;
}
else{
String scissors = "SCISSORS";
return scissors;
}
}
投掷号是由用户随机生成或输入的,并保存到throwType
public int makeThrow(){
Random rand = new Random();
int throwType = rand.nextInt(3);
this.throwType = throwType;
return throwType;
}
public int makeThrow(int throwType){
this.throwType = throwType;
return throwType;
}
最佳答案
这些是方法而不是构造函数。
您不能将任何内容传递给lastThrow
方法,因为它没有任何参数。
您的两个makeThrow()
方法都已经更改了throwType
的值,因此,下次您调用lastThrow()
时,它将返回正确的String
。