java 可变预期误差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31797631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable expected error
提问by Neuromeda
I am trying to make a bot that selects a random joke from an array list, but I seem to get an error that says:
我正在尝试制作一个从数组列表中随机选择一个笑话的机器人,但我似乎收到一个错误消息:
Variable expected
预期变量
My code so far is:
到目前为止我的代码是:
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());
}
}
the error comes up for:
错误出现在:
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
回答by Kon
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
That's some very invalid syntax. You can't assign the return
value of a method (in this case jokesIKnow.size()
is a method which returns something) to anything except a variable. For example, this is legal:
这是一些非常无效的语法。您不能将return
方法的值(在本例中jokesIKnow.size()
是返回某些内容的方法)分配给变量以外的任何内容。例如,这是合法的:
int numberOfJokes = jokesIKnow.size();
Here you are trying to assign it to another method. Perhaps you mean to write Math.random(jokesIKnow.size())
which passes the variable into the random generator.
在这里,您试图将其分配给另一种方法。也许你的意思是写Math.random(jokesIKnow.size())
哪个将变量传递给随机生成器。