在以下代码中,只要我的代码没有在文件顶部声明一个程序包,我的IDE(VS代码)就会给我一个错误:The declared package does not match the expected package “”
,但代码可以正常运行。但是,当我尝试声明该程序包时,IDE会显示错误已得到修复,但是在完成cannot find symbol
时我可能会出错。
为什么在包含cannot find symbol
时出现package [name of parent directly]
错误?
//package studentprofilechallenge;
class Main {
public static void main(String[] args) {
StudentProfile p1 = new StudentProfile("John", "Doe", 2019, 3.5, "IT");
StudentProfile p2 = new StudentProfile("Jane", "Doe", 2016, 3.3, "Public Health");
System.out.println(
"Student named " + p2.firstName + " " + p2.lastName + "\'s graduation date is "
+ p2.endYear + ".");
}
}
-
//package studentprofilechallenge;
class StudentProfile {
String firstName;
String lastName;
int endYear;
double GPA;
String major;
public static String universityName = "X University";
public StudentProfile(
String firstName,
String lastName,
int endYear,
double GPA,
String major) {
this.firstName = firstName;
this.lastName = lastName;
this.endYear = endYear;
this.GPA = GPA;
this.major = major;
}
public void incrementEndYear() {
this.endYear = this.endYear + 1;
}
public void decrementEndYear(){
this.endYear = this.endYear-1;
}
}