为了庆祝Pi Day,我决定采用Monte Carlo方法近似计算?的值。,但是我的算法似乎无效。
我尝试使用不同的参数运行,但总能获得约3.66的效果
我已经尝试调试,但无法弄清楚。
public class ApproximatePi {
private int iterations; // how many points to test
private double r; // width of the square / radius of circle (quarter circle)
private int inCount = 0; // number of points that are inside the circle
private int outCount = 0; // number of points outside of the circle
private Random getNum = new Random(System.currentTimeMillis());
ApproximatePi(int iterations, double r) {
this.iterations = …Run Code Online (Sandbox Code Playgroud) 我对使用对象没有经验,所以我真的很想输入一些信息。
我正在尝试从其中包含某些“不需要的单词”的列表中删除注释,这些评论和“不需要的单词”列表都在ArrayList对象中。
这在名为的类中FormHelper,该类包含comments作为ArrayList 的私有成员,该auditListArrayList在名为的成员函数中本地创建populateComments(),然后该成员函数调用此函数(如下)。PopulateComments()由构造函数调用,因此在FormHelper创建的实例时,此函数仅被调用一次。
private void filterComments(ArrayList <String> auditList) {
for(String badWord : auditList) {
for (String thisComment : this.comments) {
if(thisComment.contains(badWord)) {
int index = this.comments.indexOf(thisComment);
this.comments.remove(index);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
关于实现此方法的方式感觉不对,我还担心自己对ArrayList函数的使用效率低下。我的怀疑正确吗?