我无法在Eclipse中运行此程序。 Eclipse并没有说出任何错误,我无法打开它。每次我尝试,它都会在我的默认包中打开另一个程序。
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 int state;
public int getState() {
return state;
}
private RandomGenerator rgen = new RandomGenerator();
}
public class ConsecutiveHeads extends CoinFace{
public void run () {
while (true) {
int totalFlip = 0;
int consecutiveHeads = 0;
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;
}
}
}
}
答案 0 :(得分:4)
你忘了宣布一个主要的方法:
public static void main(String args[]) {
new ConsecutiveHeads().run();
}