无法 运行 在 eclipse workplace 上编程

Cannot run program on eclipse workplace

我是 java 和 eclipse 的新手,当我为下面的代码单击 运行 时,没有任何反应。弹出绿色加载栏,但没有任何反应。我试过 运行 其他代码,它们完全没问题。

import java.util.*;

public class CMIS242WK3BrunsonT {

public static void main(String[] args) {;
    
    // parent class Aircraft
    class Aircraft
    {
        // attributes
        int numberOfEngines;
        float length;
        float height;

        // constructor
        Aircraft(int numberOfEngines,float length,float height)
        {
            this.numberOfEngines=numberOfEngines;
            this.length=length;
            this.height=height;
        }
        public void fly()
        {
            System.out.println("All Aircrafts Fly");
        } 
    }

    // child class Helicopter
    class Helicopter extends Aircraft
    {
        // attributes
        float mainRotorBladeSize;
        float tailRotorBladeSize;

        // constructor
        Helicopter(int numberOfEngines,float length,float height,float mainRotorBladeSize,float tailRotorBladeSize)
        {
            super(numberOfEngines,length,height);
            this.mainRotorBladeSize=mainRotorBladeSize;
            this.tailRotorBladeSize=tailRotorBladeSize;
        }

        public void hovering(){
              System.out.println("Helicopter hovers");
        }
    }

    // child class Plane
    class Plane extends Aircraft
    {
        // attributes
        float wingSpan;

        // constructor
        Plane(int numberOfEngines,float length,float height,float wingSpan)
        {
            super(numberOfEngines,length,height);
            this.wingSpan=wingSpan;
        }
        public void gliding(){
              System.out.println("Plane Glides");
        }
    }
    class test {

        public void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            
            // creating instance of child classes 

            Helicopter helicopter=new Helicopter(2, 50, 30, 50, 40);
            helicopter.fly();
            helicopter.hovering();

            Plane plane=new Plane(4, 100, 50, 80);
            plane.fly();
            plane.gliding();

            sc.close();
        }
    }
}

}

很高兴收到有关如何解决此问题的反馈。如果需要更多详细信息,我会尽力回答您的问题。

重述代码结构:

  • 外部class
    • 静态主要
      • 本地class#1
      • 本地class#2
      • 本地class#3
      • 本地 class 'test'
        • 方法主要(不是静态的)
          • 测试代码

所以基本上你的代码什么都不做,因为方法 test.main() 从未被调用过。 无论如何,本地 class 'test' 是多余的,只需将代码从它的方法 main 移动到外部 class 的静态 main 以获得此结构:

  • 外部class
    • 静态主要
      • 本地class#1
      • 本地class#2
      • 本地class#3
      • 测试代码

更精心地改变

    // code nested in CMIS242WK3BrunsonT.main
    class test {

        public void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            
            // creating instance of child classes 

            Helicopter helicopter=new Helicopter(2, 50, 30, 50, 40);
            helicopter.fly();
            helicopter.hovering();

            Plane plane=new Plane(4, 100, 50, 80);
            plane.fly();
            plane.gliding();

            sc.close();
        }
    }

只是

        // code nested in CMIS242WK3BrunsonT.main

        Scanner sc = new Scanner(System.in);

        // creating instance of child classes

        Helicopter helicopter = new Helicopter(2, 50, 30, 50, 40);
        helicopter.fly();
        helicopter.hovering();

        Plane plane = new Plane(4, 100, 50, 80);
        plane.fly();
        plane.gliding();

        sc.close();

或者

        class test {

            public void main(String[] args) {
                // same code as above
            }
        }
        new test().main(args);

创建的 Scanner 实例也是多余的,当你不从输入中读取时,你可以删除 Scanner sc=new Scanner(System.in);sc.close();

除此之外,在学习 Java 时,我建议将每个 class 放入单独的 .java 文件中。这样你就可以更好地了解可见性的各个方面。无论如何,本地 classes 很少在实践中使用。