简体   繁体   English

无法解析为 VS Code [Java] 中的类型

[英]Cannot be resolved to a type in VS Code [Java]

I am having problems running my Dog.class and DogDemo.java files in VS Code.我在 VS Code 中运行 Dog.class 和 DogDemo.java 文件时遇到问题。 I typed it exactly as written in a Java book.我完全按照 Java 书中的内容输入了它。 I am coming up with the error:我想出了错误:

Dog cannot be resolved to a type Dog 无法解析为类型

at DogDemo.main(DogDemo.java:7)在 DogDemo.main(DogDemo.java:7)

I also have tried cleaning the java workspace, but it doesn't work.我也尝试过清理 java 工作区,但它不起作用。

EDIT: I have fixed the problem by changing the filename from Dog.class to Dog.java.编辑:我通过将文件名从 Dog.class 更改为 Dog.java 解决了这个问题。 For some reason, I thought that my book told me to use a.class file.出于某种原因,我认为我的书告诉我使用.class 文件。

Dog.class狗.class



public class Dog
{
    public String name;
    public String breed;
    public age = 0;
    

    public void writeOutput()
    {
        System.out.println("Name " + name);
        System.out.println("Breed: " + breed);
        System.out.println("Age in calendar years" + age);
        System.out.println("Age in human years" + getAgeInHumanYears());
        System.out.println();
    }

    public int getAgeInHumanYears()
    {
        int humanAge = 0;

        if (age <= 2) 
        {
            humanAge = age * 11;
        }

        else
        {
            humanAge = 22 + ((age-2) * 5);
        }

        return humanAge;
    }
}

DogDemo.java DogDemo.java



public class DogDemo 
{
    public static void main(String[] args) 
    {
        Dog kumo = new Dog();

        kumo.name = "kumo";
        kumo.age = 42;
        kumo.breed = "Corgi";
        kumo.writeOutput();

        Dog scooby = new Dog();
        scooby.name = "Scooby";
        scooby.age = 9;
        scooby.breed = "Great Dane";
        System.out.println(scooby.name + " is a " + scooby.breed +
        ".");`
        System.out.print("He is " + scooby.age + " years old, or ");

        int humanYears = scooby.getAgeInHumanYears();
        System.out.println(humanYears + " in human years.");
    }
}

In your Dog-Class you forgot the type for the age variable (probably an integer), so correct it would be:在您的 Dog-Class 中,您忘记了年龄变量的类型(可能是整数),因此正确的是:

public class Dog
{
    public String name;
    public String breed;
    public int age = 0;

And in your DogDemo-class you've got a Typo in the line:在您的 DogDemo 课程中,您有一个错字:

System.out.println(scooby.name + " is a " + scooby.breed +
    ".");``

The " ' " after the semicolon should be removed to:分号后的“'”应删除为:

System.out.println(scooby.name + " is a " + scooby.breed + ".");

Then it works just fine for me:) Keep on coding!然后它对我来说很好:) 继续编码!

Dog

class Dog {
    String name;
    String breed;
    int age;

    void writeOutput()
    {
        System.out.println("Name " + name);
        System.out.println("Breed: " + breed);
        System.out.println("Age in calendar years" + age);
        System.out.println("Age in human years" + getAgeInHumanYears());
        System.out.println();
    }

    int getAgeInHumanYears()
    {
        int humanAge = 0;

        if (age <= 2)
        {
            humanAge = age * 11;
        }

        else
        {
            humanAge = 22 + ((age-2) * 5);
        }

        return humanAge;
    }
}

DogDemo狗演示

public class DogDemo {

    public static void main(String[] args) {
        Dog kumo = new Dog();

        kumo.name = "kumo";
        kumo.age = 42;
        kumo.breed = "Corgi";
        kumo.writeOutput();

        Dog scooby = new Dog();
        scooby.name = "Scooby";
        scooby.age = 9;
        scooby.breed = "Great Dane";
        System.out.println(scooby.name + " is a " + scooby.breed + ".");
        System.out.print("He is " + scooby.age + " years old, or ");

        int humanYears = scooby.getAgeInHumanYears();
        System.out.println(humanYears + " in human years.");
    }
}

The changes I made:我所做的更改:

  1. removed public keyword from your data class Dog从您的数据中删除公共关键字 class Dog
  2. you missed ` on your main function where you are printing out name + breed您在主要的 function 上错过了 ` 打印名称 + 品种
  3. add type int to the age in Dog's propertyage的属性中添加 int 类型

please also make sure your classes are in the same package or import it into your class if they are not.还请确保您的课程在同一个 package 中,或者如果不是,则将其导入您的 class 中。

EDIT:编辑:

I think other people also had the same issues as you are having you can find it here我认为其他人也遇到了与您相同的问题,您可以在这里找到

And also you can follow the steps here , this documentation provided by VS code.您也可以按照此处的步骤操作,此文档由 VS 代码提供。 You can see which steps are missing.您可以查看缺少哪些步骤。

you forgot to state the type of your variable age你忘了 state 你的变量age的类型

    public age = 0;

should become应该成为

    public int age = 0;

In addition on DogDemo.java lines 17-18 you have除了 DogDemo.java 第 17-18 行,您还有

        System.out.println(scooby.name + " is a " + scooby.breed +
        ".");`

you have a backtick `, which isn't valid and should be removed to become你有一个反引号`,它是无效的,应该被删除成为

        System.out.println(scooby.name + " is a " + scooby.breed +
        ".");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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