找不到*类名*的符号? Java的

时间:2017-11-20 23:06:19

标签: java visual-studio

这两个类都在同一个包中,所以我很困惑为什么它不起作用。它确实在netbeans中工作,但我现在使用的是Visual Studio Code。我将包含代码和我得到的错误。

分类(包含主要方法)

package project;

import java.util.ArrayList;
import java.util.Scanner;


public class Division
{
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args)
    {


        ArrayList<Fighter> fighters = new ArrayList();

        fighters.add(new Fighter("Conor McGregor", 29, "Ireland", 21, 3));
        fighters.add(new Fighter("Gunnar Nelson", 28, "Iceland", 16, 3));
        fighters.add(new Fighter("Stipe Miocic", 33, "USA", 17, 2));
        fighters.add(new Fighter("Cody Garbrandt", 26, "USA", 11, 0));
        fighters.add(new Fighter("Demetrious Johnson", 30, "USA", 27, 2));
        fighters.add(new Fighter("Jose Aldo", 31, "Brazil", 26, 3));
        fighters.add(new Fighter("George St Pierre", 40, "Canada", 25, 2));
        fighters.add(new Fighter("Fabricio Werdum", 40, "Brazil", 22, 7));
        fighters.add(new Fighter("Michael Bisping", 39, "United Kingdom", 30, 7));


        displayAllFighters(fighters);


    }

    //Adds fighter to ArrayList
    public static void addFighter(ArrayList<Fighter> fighters)
    {
        System.out.print("Please enter fighters name: \t");
        String name = scanner.nextLine();

        System.out.print("\nPlease enter fighters age: \t");
        int age = scanner.nextInt();

        scanner.nextLine();

        System.out.print("\nPlease enter fighters country: \t");
        String country = scanner.nextLine();

        System.out.print("\nPlease enter amount of wins: \t");
        int wins = scanner.nextInt();

        System.out.print("\nPlease enter amount of losses: \t");
        int losses = scanner.nextInt();

        fighters.add(new Fighter(name, age, country, wins, losses));

        System.out.println("Fighter Added!");
    }

    //Removes a fighter from ArrayList
    public static void removeFighter(ArrayList<Fighter> fighters)
    {
        System.out.print("Please enter the name of the fighter you wish to remove: \t");
        String name = scanner.nextLine();

        for (Fighter fighter : fighters)
        {
            if (fighter.getName() == name)
            {
                fighters.remove(fighter);
            }
        }
    }

    //Display info of all fighters
    public static void displayAllFighters(ArrayList<Fighter> fighters)
    {
        for (Fighter fighter : fighters)
        {
            System.out.println(fighter);
            System.out.println("==========================================");
        }
    }

    //Counts amount of fighters with a win % that is less that entered value
    public static int countWinPercentLowerThan(ArrayList<Fighter> fighters , int value)
    {
        int count = 0;

        for (Fighter fighter : fighters)
        {
            if (fighter.getPercent() < value)
            {
                count++;
            }
        }
        return count;
    }

    //Counts amount of fighters with a win % that is greater that entered value
    public static int countWinPercentGreaterThan(ArrayList<Fighter> fighters , int value)
    {
        int count = 0;

        for (Fighter fighter : fighters)
        {
            if (fighter.getPercent() > value)
            {
                count++;
            }
        }
        return count;
    }

}

战斗机类(编译时无法识别)

package project;

public class Fighter
{
    private String name;
    private int age;
    private String country;
    private int wins;
    private int losses;
    private String weightClass;

    public Fighter(String name, int age, String country, int wins, int losses, String weightClass)
    {
        this.name = name;
        this.age = age;
        this.country = country;
        this.wins = wins;
        this.losses = losses;
        this.weightClass = weightClass;
    }

    public Fighter(String name)
    {
        this.name = name;
        this.age = 0;
        this.country = "TBA";
        this.wins = 0;
        this.losses = 0;
        this.weightClass = "TBD";
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return this.age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public String getCountry()
    {
        return this.country;
    }

    public void setCountry(String country)
    {
        this.country = country;
    }

    public int getWins()
    {
        return this.wins;
    }

    public void setWins(int wins)
    {
        this.wins = wins;
    }

    public int getLosses()
    {
        return this.losses;
    }

    public void setLosses(int losses)
    {
        this.losses = losses;
    }

    public String toString()
    {
        return "Fighter Name: " + this.name + ".\nFighter age: " + this.age + ".\nFighter nation: " + this.country + ".\nFighter wins: " + this.wins + ".\nFighter losses: " + this.losses + ".";
    }

    public void updateWin()
    {
        this.wins++;
    }

    public void updateLosses()
    {
        this.losses++;
    }

    public double getPercent()
    {
        int totalFights = this.wins + this.losses;
        double percent = this.wins * 100/totalFights;
        return percent;
    }

}

错误讯息:

c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:34: error: cannot find symbol
    public static void addFighter(ArrayList<Fighter> fighters)
                                            ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:59: error: cannot find symbol
    public static void removeFighter(ArrayList<Fighter> fighters)
                                               ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:74: error: cannot find symbol
    public static void displayAllFighters(ArrayList<Fighter> fighters)
                                                    ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:84: error: cannot find symbol
    public static int countWinPercentLowerThan(ArrayList<Fighter> fighters , int value)
                                                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:99: error: cannot find symbol
    public static int countWinPercentGreaterThan(ArrayList<Fighter> fighters , int value)
                                                           ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:15: error: cannot find symbol
        ArrayList<Fighter> fighters = new ArrayList();
                  ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:17: error: cannot find symbol
        fighters.add(new Fighter("Conor McGregor", 29, "Ireland", 21, 3));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:18: error: cannot find symbol
        fighters.add(new Fighter("Gunnar Nelson", 28, "Iceland", 16, 3));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:19: error: cannot find symbol
        fighters.add(new Fighter("Stipe Miocic", 33, "USA", 17, 2));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:20: error: cannot find symbol
        fighters.add(new Fighter("Cody Garbrandt", 26, "USA", 11, 0));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:21: error: cannot find symbol
        fighters.add(new Fighter("Demetrious Johnson", 30, "USA", 27, 2));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:22: error: cannot find symbol
        fighters.add(new Fighter("Jose Aldo", 31, "Brazil", 26, 3));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:23: error: cannot find symbol
        fighters.add(new Fighter("George St Pierre", 40, "Canada", 25, 2));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:24: error: cannot find symbol
        fighters.add(new Fighter("Fabricio Werdum", 40, "Brazil", 22, 7));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:25: error: cannot find symbol
        fighters.add(new Fighter("Michael Bisping", 39, "United Kingdom", 30, 7));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:53: error: cannot find symbol
        fighters.add(new Fighter(name, age, country, wins, losses));
                         ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:64: error: cannot find symbol
        for (Fighter fighter : fighters)
             ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:76: error: cannot find symbol
        for (Fighter fighter : fighters)
             ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:88: error: cannot find symbol
        for (Fighter fighter : fighters)
             ^
  symbol:   class Fighter
  location: class Division
c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java:103: error: cannot find symbol
        for (Fighter fighter : fighters)
             ^
  symbol:   class Fighter
  location: class Division
Note: c:\Users\Cian Tiernan\OneDrive - Dundalk Institute of Technology\College\Object Orientated Programming\NetBeansProjects\Java\src\project\Division.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

1 个答案:

答案 0 :(得分:-1)

我看到的第一个错误是构造函数需要加权类参数,但是当您使用Fighter类初始化arraylist时,它缺少该值。

例如。 fighters.add(新战斗机(“Conor McGregor”,29,“爱尔兰”,21,3 )); 缺少体重等级。