我在我的Java练习书中创建了一个小游戏,当我在Eclipse中编译它运行正常时,但是一旦我添加main方法并将其作为.jar导出到我的桌面上,当我双击时它就不起作用它,它也不能在命令控制台上运行。我似乎无法导出任何funtioning .jar程序,尽管它们在Eclipse中编译得很好。我假设在使用main方法时我做错了什么?
import acm.program.*;
import acm.util.*;
import java.awt.Color;
import acm.graphics.*;
public class CrapsGameTest extends ConsoleProgram{
public static void main (String[] args){
CrapsGameTest craps = new CrapsGameTest();
craps.run();
}
public void run() {
/*if total = 2, 3, or 12 you loose
* if total = 7 or 11 you win
*
* */
setFont("Helvetica-40");
int total = rollTwoDice();
if (total == 2 || total == 3 || total==12) {
println(total +" You loose");
} else if (total == 7 || total == 11) {
println(total +" You win");
} else {
//saves points under the initial TOTAL variable
int points=total;
println("your point total is " + points);
//initializes step by step rolls with i++
int i = 0;
while(i>=0) {
int x = readInt("Roll again?");
if (x==1) {
i++;
}
//changes the total variable by rolling dice.
//if the new total variable = points then you win:
//after every loop "total" variable changes due to the rollTwoDice method.
total = rollTwoDice();
if (points ==total) {
println("You win!");
break;
} else if (total == 7) {
println("you loose");
break;
}
}
}
int y = readInt("Do you wish to play again?");
if (y == 1) {
run();
}
}
private int rollTwoDice() {
int d1 = rgen.nextInt(1,6);
int d2 = rgen.nextInt(1,6);
int total = d1 + d2;
println("Rolling dice: " + d1 + " + "+d2 + " = " + total );
return total;
}
//initializes rgen variable
private RandomGenerator rgen = new RandomGenerator();
}
答案 0 :(得分:0)
Java程序需要从类中的特定方法开始,即 public static void main(String [] args)方法。如果你的程序使用 在ACM库中,您需要编辑代码以获得main()。 只需将以下代码添加到即可轻松完成此操作 具有public void run()的类,用其名称替换 下面的课程。
public static void main (String[] args){
new CrapsGameTest().start(args);
}
经过那么小的改变,你的出口应该有效。