问题描述
我有一个这样的枚举类:
public enum Elementi {
IDROGENO("H", 1, 1.008),
ELIO("He", 2, 4.003),
// ... altri elementi
LITIO("Li", 3, 6.491);
private int numeroAtomico;
private double massaAtomica;
private String simbolo;
public int getNumeroAtomico() {
return numeroAtomico;
}
public String getSimbolo() {
return simbolo;
}
private Elementi(String simbolo, int numeroAtomico, double massaAtomica){
this.simbolo = simbolo;
this.numeroAtomico = numeroAtomico;
this.massaAtomica = massaAtomica;
}
}
在另一个文件中,我有一个像这样的主程序:
import java.util.Elementi;
public class Main{
public static void main (String[] args){
for (Elementi e: Elementi.values())
System.out.println("%s\t|\t%d|\t%s\n", e.getSimbolo(),e.getNumeroAtomico(), e);
}
}
如果我编译并执行两个文件,则会收到以下错误:
-Main.java:1: error: cannot find symbol
import java.util\\\.\\\Elementi;
symbol: class Elementi
location: package java.util
-Main.java:7: error: no suitable method found for println (String, String, int, Elementi)
System.out\\\.\\\println("%s\t|\t%d|\t%s\n", e.getSimbolo(), e.getNumeroAtomico(), e);
Method PrintStream.println(Object) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(String) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(char[]) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(double) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(float) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(long) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(int) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(char) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println(boolean) is not applicable
(actual and formal argument lists differ in lenght)
Method PrintStream.println() is not applicable
(actual and formal argument lists differ in lenght)
2 errors
我将两个错误放在\\\\之间,如下所示:\\\\ error \\\\
我会解决这些问题
谢谢大家
1楼
-
我认为您的
Elementi
不在java.util
包中。 用正确的包装进口。 -
System.out.println()
不使用varargs和格式字符串。 请改用System.out.printf()
。