就目前而言,控制台允许我编译,但不允许运行它,它说:
“错误:无法找到或加载主类 MonsterFight”
代码如下:
class Fight {
Random rand= new Random();
int Hit (int x) {
int numHit = rand.nextInt(100);
return (int) x - numHit;
}
class MonsterFight {
public void main(String [] args){
String name;
int hp = 1000;
System.out.println("You start at 1000 Hitpoints.");
Fight battle = new Fight();
while (hp != 0) {
hp = Hit(hp);
System.out.println("You have now " + hp + " hitpoints.");
}
}
}
}
我似乎无法让它发挥作用。感谢所有帮助,也感谢使这个更干净的提示,因为我对 Java 相当陌生。
最佳答案
声明主方法static
并使MonsterFight
成为顶级类(因为静态方法只能在后者中声明):
class MonsterFight {
public static void main(String [] args){
...
}
}
关于java - 我该如何让这个 'monster fight simulator' 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19761710/