我正在尝试制作一个从数组列表中选择一个随机笑话的机器人,但是似乎出现了一条错误消息:


  预期变量


到目前为止,我的代码是:

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

最佳答案

Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());


那是一些非常无效的语法。您不能将方法的return值(在这种情况下jokesIKnow.size()是一种返回值的方法)分配给变量以外的任何东西。例如,这是合法的:

int numberOfJokes = jokesIKnow.size();


在这里,您尝试将其分配给另一种方法。也许您的意思是编写Math.random(jokesIKnow.size()),它将变量传递到随机生成器中。

08-06 05:27