简体   繁体   English

在Visual Studio代码编译错误中调试Java

[英]Debugging Java in Visual Studio Code Compilation Error

I'm trying to debug a simple Java program using the Java Extension Pack in VS code. 我正在尝试使用VS代码中的Java Extension Pack调试一个简单的Java程序。 I am pretty new to Java programming and I read up on Writing Java in VS code https://code.visualstudio.com/docs/java/java-tutorial to understand how to debug Java code. 我是Java编程的新手,我读了《用VS代码编写Java》 https://code.visualstudio.com/docs/java/java-tutorial,以了解如何调试Java代码。 I am able to run the code but when I have my class BicycleDemo as public not package(default) access modifier I have an "Exception in thread "main" java.lang.Error: Unresolved compilation problem:" 我能够运行代码,但是当我将我的类BicycleDemo作为公共类而不是程序包(默认)访问修饰符时,我遇到了“线程“主”中的异常” java.lang.Error:未解决的编译问题:”

If I have my class BicycleDemo without the public keyword it works fine. 如果我的类BicycleDemo没有公共关键字,则可以正常工作。 Why is this? 为什么是这样?

class Bicycle {
    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) { cadence = newValue; }
    void changeGear(int newValue) { gear = newValue; }
    void speedUp(int increment) { speed = speed + increment; }
    void applyBrakes(int decrement) { speed = speed - decrement; }
    void printStates() {
    System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear);
    }
}

public class BicycleDemo {
public static void main(String[] args) {
    // Create two different Bicycle objects
    Bicycle bike1 = new Bicycle();
    Bicycle bike2 = new Bicycle();
    // Invoke methods on those objects
    bike1.changeCadence(50);
    bike1.speedUp(10);
    bike1.changeGear(2);
    bike1.printStates();
    bike2.changeCadence(50);
    bike2.speedUp(10);
    bike2.changeGear(2);
    bike2.changeCadence(40);
    bike2.speedUp(10);
    bike2.changeGear(3);
    bike2.printStates();
    }
}

A public class must be declared in a .java file with the same name (and be located in a directory structure that corresponds to the package name, if any). 公共class必须在具有相同名称的.java文件中声明(并且位于与包名称相对应的目录结构中,如果有的话)。

Your problem is probably that the file containing the public BicycleDemo is not named BicycleDemo.java . 您的问题可能是包含公共BicycleDemo的文件未命名为BicycleDemo.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM