Java 我如何制作这个';怪物战斗模拟器';工作
现在,console允许我编译,但不允许运行它,它说:Java 我如何制作这个';怪物战斗模拟器';工作,java,class,methods,simulator,Java,Class,Methods,Simulator,现在,console允许我编译,但不允许运行它,它说: “错误:无法找到或加载主类” 代码如下: 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 []
“错误:无法找到或加载主类” 代码如下:
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
成为顶级类(因为静态方法只能在后者中声明):
声明main方法
static
,并使MonsterFight
成为顶级类(因为静态方法只能在后者中声明):
将MonsterFight作为公共外部类,并且主方法签名应该是
public static void main(String [] args){
注意:有适当的while循环条件
试试这个
import java.util.Random;
class Fight {
static int Hit (int x) {
Random rand= new Random();
int numHit = rand.nextInt(100);
return (int) x - numHit;
}
}
public class MonsterFight {
public static 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 = Fight.Hit(hp);
System.out.println("You have now " + hp + " hitpoints.");
}
}
}
将MonsterFight作为公共外部类,并且主方法签名应该是
public static void main(String [] args){
注意:有适当的while循环条件
试试这个
import java.util.Random;
class Fight {
static int Hit (int x) {
Random rand= new Random();
int numHit = rand.nextInt(100);
return (int) x - numHit;
}
}
public class MonsterFight {
public static 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 = Fight.Hit(hp);
System.out.println("You have now " + hp + " hitpoints.");
}
}
}