我很困惑如何保存从“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;
}
}
throw 数字要么是随机生成的,要么是由用户输入并保存到 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()
时,它将返回正确的字符串
。
关于java - 如何保存一个方法的返回值,然后将其传递给另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34780178/