2017-02-27 93 views
1

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

} 
+1

我不是Java程序員,但我會冒險猜測循環是無限的,因爲沒有更新循環內的'condition'變量。另外請記住,使用'do ... while'循環意味着代碼塊將至少執行一次,無論條件如何 - 請確保這是您想要的。 – Damon

+0

你說得對,這是我的問題的一個重要部分。我發現了另一個問題,即使我修正了這個問題,我也會遇到這樣的問題:我將hitPointsCounter定義爲(x/y)> = sqrt(2)的方法不允許我達到容錯級別的錯誤。 我跟我的教授聊過,他只是在圖片上留下了一個'x'出布爾方程。所以它是y

回答

0

所以我得到了代碼工作,這個問題是雙重的 - 我的代碼沒有重新評估布爾「條件」的循環中引起一個無限循環,我定義hitPointCounter以前是不正確的方式。謝謝Damon的幫助。當我將這個塊:我的環內

  percentHit = (((double)hitPointsCounter/(double)totalPointsCounter)); 
      approximateArea =(percentHit)*(referenceArea); 
      error = Math.abs(exactArea - approximateArea); 
      condition = error > tolerance; 

,改變

 if (y==0 && x==0){hitPointsCounter++;} 
    if (y>0){ 
    if ((x/y)>=Math.pow(2, 0.5)){ 

      hitPointsCounter++;} 
    } 

到:

if (y<(Math.pow(2,-0.5)*x)){ 

       hitPointsCounter++;} 

的代碼工作就像一個魅力。