我正在尝试制作一个从数组列表中选择一个随机笑话的机器人,但我似乎得到一个错误,上面写着:
预期变量
到目前为止我的代码是:
package com.delta.objects;
import java.util.ArrayList;
/**
* Created by WILLIAM on 8/3/2015.
*/
public class JokeBot extends Bot {
public ArrayList<Joke> jokesIKnow = null;
public JokeBot(ArrayList<Joke> jokesIKnow) {
this.jokesIKnow = jokesIKnow;
}
public void tellJoke(){
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
int randomNum = randomNumDouble.intValue();
}
protected void sayJoke(Joke aJoke){
talk(aJoke.getJokeSetup());
talk(aJoke.getJokePunchline());
}
}
出现错误:
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
答案 0 :(得分:4)
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
这是一些非常无效的语法。您不能将方法的return
值(在这种情况下jokesIKnow.size()
是一个返回内容的方法)分配给除变量之外的任何内容。例如,这是合法的:
int numberOfJokes = jokesIKnow.size();
在这里,您尝试将其分配给另一种方法。也许你的意思是写Math.random(jokesIKnow.size())
将变量传递给随机生成器。