Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何制作这个';怪物战斗模拟器';工作_Java_Class_Methods_Simulator - Fatal编程技术网

Java 我如何制作这个';怪物战斗模拟器';工作

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 []

现在,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 [] 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.");
        }
    }
}