Professor's equation for the boolean condition that satisfies "hitPointsCounter" in my codeY = 2 ^的積分的蒙特卡洛近似( - 0.5)x在0到1個的布爾值未正確
工作在我正在與解決幾個蒙特卡洛逼近幾何任務的Java類區。我對此的第一個實驗我有一個名爲「條件」的布爾運行一個do-while循環,當(錯誤<寬容)滿足時 - 截至目前,當我運行我的程序時,這似乎正在產生一個無限循環。
當我將布爾型的「while」語句更改爲計數器時,我可以讓它正常運行,所以我知道布爾本身存在問題。
如果要求我從0到1的高度2 ^( - 0.5)求解平方的面積,而教授給我的增加我的hitPointsCounter變量的條件將工作得很好, = 2 ^( - 0.5)x - 因此,我即興創作並注意到,對於一個點在該線上或之下,它必須由x/y> = 2^0.5滿足。
任何人都可以給我一些反饋,我可能已經搞砸了?我的問題是在開關情況1和休息之間的某處。
public static void main(String[] args){
double tolerance = 1E-9, referenceArea =0.0, exactArea =0.0, x =0.0, y =0.0, error = 0.0, approximateArea = 0.0;
int totalPointsCounter =0, hitPointsCounter =0;
char firstCharacter ;
String titleText, messageText;
titleText = "The Monte Carlo Method";
int userInput = JOptionPane.showConfirmDialog(null, "Run a Monte Carlo Experiment?", titleText, JOptionPane.YES_NO_OPTION);
if (userInput == 1){
JOptionPane.showMessageDialog(null, "The program terminates \nGood Bye!", titleText, JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
String userExperimentInput = JOptionPane.showInputDialog(null, "Please Enter\n1 for Experiment 1\n2 for Experiment 2\n3 for Experiment 3\n4 for Experiment 4", titleText, JOptionPane.QUESTION_MESSAGE).trim();
if(userExperimentInput == null || userExperimentInput.equals("")){
JOptionPane.showMessageDialog(null, "No input received\nThe program terminates", titleText, JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
char theCase = userExperimentInput.charAt(0);
switch (theCase){
case '1':
exactArea = Math.pow(2, -0.5)*1/2;
referenceArea = 1.0;
boolean condition = error < tolerance;
double percentHit = (((double)hitPointsCounter/(double)totalPointsCounter));
approximateArea =(percentHit)*(referenceArea);
error = exactArea - Math.abs(approximateArea);
int counter = 0;
do {
x= Math.random();
y= Math.random();
totalPointsCounter++;
if (y==0 && x==0){hitPointsCounter++;}
if (y>0){
if ((x/y)>=Math.pow(2, 0.5)){
hitPointsCounter++;}
}
exactArea = Math.pow(2, -0.5)*1/2;
referenceArea = 1.0;
counter++;
}
while(condition);
percentHit = (((double)hitPointsCounter/(double)totalPointsCounter));
approximateArea =(percentHit)*(referenceArea);
error = exactArea - Math.abs(approximateArea);
messageText ="Experiment #1" + ": \n\nMC needed " + totalPointsCounter + " random points for tolerance " + tolerance + "\nThe approximate area is " + approximateArea;
JOptionPane.showMessageDialog(null, messageText, titleText, JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
exactArea = Math.PI;
referenceArea = 4.0;
case 3:
exactArea = (1.0/3.0);
referenceArea = 2.0;
case 4:
exactArea = 2;
referenceArea = Math.PI;
default:
System.out.println("Wrong character for case number, program terminates");
System.exit(0);
}
}
我不是Java程序員,但我會冒險猜測循環是無限的,因爲沒有更新循環內的'condition'變量。另外請記住,使用'do ... while'循環意味着代碼塊將至少執行一次,無論條件如何 - 請確保這是您想要的。 – Damon
你說得對,這是我的問題的一個重要部分。我發現了另一個問題,即使我修正了這個問題,我也會遇到這樣的問題:我將hitPointsCounter定義爲(x/y)> = sqrt(2)的方法不允許我達到容錯級別的錯誤。 我跟我的教授聊過,他只是在圖片上留下了一個'x'出布爾方程。所以它是y