簡體   English   中英

程序不能作為Java Applet運行

[英]Program can not be run as Java Applet

我編寫了一個無法作為Java Applet運行的簡單程序。 當我嘗試運行程序時,會彈出一個框架並詢問要運行哪個程序。 我點擊了我寫的程序,如下所示。 令人驚訝的是,Eclipse會在同一個默認包中運行其他程序。 誰能告訴我出了什么問題?

這是Java的藝術與科學第6章練習3中的一個程序:

ApproxPIValue

import acm.util.*;

public class ApproxPIValue {

public void run() {
    int total = 0; //This variable counts the amount of time x^2 + y^2 < 1.//
    for (int a = 0; a < 10000; a++) { 
        double x = rgen.nextDouble(-1.0, 1.0);
        double y = rgen.nextDouble(-1.0, 1.0);
        if (Math.sqrt(x) + Math.sqrt(y) < 1) { //x^2 + y^2 < 1 means that     this pair number will fall into the circle with radius of 1 centered in the middle     of the coordinates.  
            total++;
        }
        double approxPIValue = total / 10000 * 4; //total / 100000 is the approximate ratio of the area of the circle over the area of the square. The approximate ratio would be close to PI/4 if x and y are randomly chosen. So total / 10000 * 4 will give the approximate PI.//
        System.out.print(approxPIValue);
    }
}

/* set RandomGenerator as an instance variable. */
private RandomGenerator rgen = new RandomGenerator();
}

我還想提出另一個無效的程序。

CoinFace

import acm.util.*;

/**
 * This class decides the face of a coin. 
 * 1 and 2 represent correspondingly Heads and Tails.
 * Clients can get the "face" of the coin by calling getState() method.
 */

public class CoinFace {

public CoinFace() {
    state = rgen.nextInt(1, 2);
}


/* private instance variable.  */
private int state;

public int getState() {
    return state;
}

/* declare RandomGenerator as an instance variable. */
private RandomGenerator rgen = new RandomGenerator();
}

ConsecutiveHeads

public class ConsecutiveHeads extends CoinFace{
public void run () {
    while (true) {
        int totalFlip = 0;
        int consecutiveHeads = 0; //the time of getting consecutive Heads when flipping a coin.//
        CoinFace a = new CoinFace();
        if (a.getState() == 1) {
            System.out.print("Heads");
            totalFlip++;
            consecutiveHeads++;
        } else if (consecutiveHeads == 3) {
            System.out.print("It took " + totalFlip + " to get 3 consecutive heads." );
            break;
        } else {
            System.out.print("Tails");
            consecutiveHeads = 0;
            totalFlip++;
        }
    }
}
}

由於我無法運行該程序,我不知道程序將如何運行。 在此先感謝任何有關改進計划的解決方案和建議!

ACM JApplet顯然被稱為Program 一個應用程序 必須擴展Program如果它要嵌入到網頁中。

但為什么要編寫applet? 如果是由於規格。 請教老師, 為什么CS老師應該停止教Java小程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM