当前位置: 代码迷 >> java >> 使用枚举参数和导入错误的Println错误
  详细解决方案

使用枚举参数和导入错误的Println错误

热度:44   发布时间:2023-07-26 14:11:58.0

我有一个这样的枚举类:

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包中。 用正确的包装进口。
  2. System.out.println()不使用varargs和格式字符串。 请改用System.out.printf()
  相关解决方案