错误:只有在显式请求注解处理时才会接受类名
我做了这个计算器应用,是为了在我的APCSA课程中计算墙面需要的油漆量,或混凝土板上的厚度。不过,当我尝试运行它时,遇到了这个错误信息
error: Class names, 'MaterialsCalculatorApp.Java', are only accepted if annotation processing is explicitly requested.
有人能弄清楚怎么修复吗?
下面是代码,供参考(我们这节课还没学过Javac)。
MaterialsCalculatorApp.Java
import java.util.Scanner;
public class MaterialsCalculatorApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("===== Materials Calculator =====");
System.out.println("1. Paint Wall");
System.out.println("2. Concrete Slab");
System.out.print("Choose project type (1 or 2): ");
int choice = sc.nextInt();
MaterialProject project = null;
if (choice == 1) {
project = new PaintWallProject();
} else if (choice == 2) {
project = new ConcreteSlabProject();
} else {
System.out.println("Invalid choice.");
sc.close();
return;
}
project.collectInput(sc);
double materialNeeded = project.calculateMaterialNeeded();
System.out.println("\n----- RESULTS -----");
System.out.println("Project Type: " + project.getName());
System.out.printf("Material Needed: %.2f %s\n",
materialNeeded,
project.getUnit());
double cost = project.calculateEstimatedCost(materialNeeded);
System.out.printf("Estimated Cost: $%.2f\n", cost);
sc.close();
}
}
MaterialProject.java
import java.util.Scanner;
public abstract class MaterialProject {
private String name;
private String unitLabel;
private double costPerUnit;
public MaterialProject(String name, String unitLabel, double costPerUnit) {
this.name = name;
this.unitLabel = unitLabel;
this.costPerUnit = costPerUnit;
}
public String getName() {
return name;
}
public String getUnitLabel() {
return unitLabel;
}
public double getCostPerUnit() {
return costPerUnit;
}
public abstract void collectInput(Scanner sc);
public abstract double calculateMaterialNeeded();
public double calculateEstimatedCost(double amountNeeded) {
return amountNeeded * costPerUnit;
}
}
PaintWallProject.java
import java.util.Scanner;
public class PaintWallProject extends MaterialProject {
private double width;
private double height;
private int coats;
private static final double COVERAGE = 350.0; // sq ft per gallon
public PaintWallProject() {
super("Paint Wall", "gallons", 34.99);
}
@Override
public void collectInput(Scanner sc) {
System.out.print("Enter wall width (feet): ");
width = sc.nextDouble();
System.out.print("Enter wall height (feet): ");
height = sc.nextDouble();
System.out.print("Enter number of coats: ");
coats = sc.nextInt();
}
@Override
public double calculateMaterialNeeded() {
double area = width * height;
double totalArea = area * coats;
return totalArea / COVERAGE;
}
}
ConcreteSlabProject.java
import java.util.Scanner;
public class ConcreteSlabProject extends MaterialProject {
private double length;
private double width;
private double thicknessInches;
public ConcreteSlabProject() {
super("Concrete Slab", "cubic yards", 150.0);
}
@Override
public void collectInput(Scanner sc) {
System.out.print("Enter slab length (feet): ");
length = sc.nextDouble();
System.out.print("Enter slab width (feet): ");
width = sc.nextDouble();
System.out.print("Enter slab thickness (inches): ");
thicknessInches = sc.nextDouble();
}
@Override
public double calculateMaterialNeeded() {
double thicknessFeet = thicknessInches / 12.0;
double cubicFeet = length * width * thicknessFeet;
return cubicFeet / 27.0; // convert to cubic yards
}
}
解决方案
文件名必须以 小写字母 .java。
你把文件命名为 MaterialsCalculatorApp.Java——那个大写的J 是问题所在。
将文件改名为一个完全不同的名字,然后再改成 MaterialsCalculatorApp.java。不要一次性就改成目标名字——那样通常行不通;大多数文件系统对大小写不敏感,直接这样改不会有什么效果。
接着你还会遇到其他错误,但Stack Overflow的设计是一次只回答一个问题。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。