首页 > 解决方案 > 我更新到 jdk 11,现在我的代码抛出错误,我不知道为什么,这对我来说都是正确的

问题描述

我刚刚在 vscode 上更新到 jdk 11,它抛出了我以前从未见过的错误。我真的很确定我的代码是正确的,但它谈论的是错位的构造函数和东西,我就是不明白。 我的代码抛出错误

import java.util.PriorityQueue;
import java.util.Arrays;
public class Huffman() {
    public Node BuildHuffmanCodeTree(String[] chars, int[] weights) {
        for(int i = 0; i < chars.length; i++) {
            Node tempNode = new Node(chars[i], weights[i]);
             
        }
    }    
    public static void main(String[] args) {
        
    }
}

这是 Node 的代码,以防万一。Node 和 Huffman 都位于同一个文件夹中,所以 Huffman 应该能够看到 Node,对吧?

import java.util.Comparator;
public class Node{
    Node child1;
    Node child2;
    Boolean isLeaf;
    int weight;
    String letter;

    public Node(Node node1, Node node2) {
        this.child1 = node1;
        this.child2 = node2;
        this.weight = node1.getWeight() + node2.getWeight();
        this.letter = null;
        this.isLeaf = false;
    }

    public Node(Node node1) {
        this.child1 = node1;
        this.child2 = null;
        this.isLeaf = false;
        this.weight = node1.getWeight();
    }
    public Node(String character, int num) {
        this.child1 = null;
        this.child2 = null;
        this.isLeaf = true; 
        this.weight = num;
        this.letter = character;
    }

    public int getWeight() {
        return this.weight;
    }

    public String getChar() {
        return this.letter;
    }
    
}

标签: javadebuggingvisual-studio-codejava-11

解决方案


()当您将其定义为时不需要类名,public class Name并且您的方法public Node BuildHuffmanCodeTree(String[] chars, int[] weights)不是类型,void因此它需要返回语句。更改Huffman.java为以下内容应该没有错误:

import java.util.PriorityQueue;
import java.util.Arrays;

public class Huffman {
    public Node BuildHuffmanCodeTree(String[] chars, int[] weights) {
        for(int i = 0; i < chars.length; i++) {
            Node tempNode = new Node(chars[i], weights[i]);
             
        }
        return null; //Not clear about your wanted function, but return sentence is needed here
    }    
    public static void main(String[] args) {
        
    }
}

推荐阅读