简体   繁体   中英

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. 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. 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:"

If I have my class BicycleDemo without the public keyword it works fine. 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).

Your problem is probably that the file containing the public BicycleDemo is not named BicycleDemo.java .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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