【问题标题】:cannot find symbol method printinfo? [duplicate]找不到符号方法 printinfo? [复制]
【发布时间】:2019-04-16 16:10:34
【问题描述】:

我无法打印出糖果条的信息。每当我尝试使用该方法时,都会出现错误:cannot find symbol-method printINFO

我尝试过使用n.printInfo()system.out.println(n.printinfo()),但还是不行。我能做什么?

方法类:

public class CandyBar
{
    public enum Color
    {
        BLUE, BROWN, GOLD, GREEN, ORANGE,
        PURPLE, RED, SILVER, WHITE, YELLOW
    }

    // instance variables
    private String name;
    private double weight;
    private Color wrapper;

    /**
     * Constructor for objects of class CandyBar
     */
    public CandyBar(String n, Color c, double w)
    {
        this.name = n;
        this.weight = w;
        this.wrapper = c;
    }

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

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

    public Color getWrapperColor()
    {
        return this.wrapper;
    }

    public void printInfo()
    {
        System.out.println(this.name);
        System.out.printf("%.1f oz with a ", this.weight);
        System.out.println(this.wrapper + " wrapper");
    }
}

主类:

import java.util.ArrayList;

public class Lab16
{
    public static void main(String[] args)
    {
        ArrayList<CandyBar> bars = new ArrayList<CandyBar>();
        addCandyBarsToList(bars);

        /**
         * Use the methods you wrote below to answer all of
         * the following questions.
         */

        /**
         * Part A:
         */

        /**
         * Is Twix in the list?
         * If so, print all information about it.
         * If not, print a message to the user.
         */

        /**
         * Is Mounds in the list?
         * If so, print all information about it.
         * If not, print a message to the user.
         */


        /**
         * Part B:
         */

        /**
         * Print all the names of candy bars with yellow wrappers.
         */


        /**
         * Part C:
         */

        /**
         * Count how many candy bars are 1.75 oz or more.
         */


        /**
         * Part D:
         */

        /**
         * Is there a candy bar that is 1.86 oz?
         * If so, print the information.
         * If not, print a message to the user.
         * Write a binary search method to get the answer.
         */
    }

    /**
     * This method searches a list to find a candy bar by name.
     *
     * @param   list    the list to search
     * @param   n       a name to find
     * @return          the index of the candy bar 
     */
    public static int findCandyBar(ArrayList<CandyBar> list, String n)
    {
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getName() == n){
                return n.printInfo(i);
            }else{
                System.out.println("The list doesnt have that Candy");
            }
        }
        return 1;
    }
    /**
     * This method prints the names of the candy bars that have a certain
     * wrapper color.
     *
     * @param   list    the list to search
     * @param   c       the color wrapper to find 
     */
    public static void findByColor(ArrayList<CandyBar> list, CandyBar.Color c)
    {

    }

    /**
     * This method counts the number of candy bars that weigh greater than
     * or equal to the weight input parameter.
     *
     * @param   list    the list to search
     * @param   w       the weight to compare to
     * @return          the count of candy bars 
     */
    public static int countByWeight(ArrayList<CandyBar> list, double weight)
    {
        return 0;
    }

    /**
     * This method searches a list using binary search.
     *
     * @param   list    the list to search
     * @param   first   the first index
     * @param   last    the last index
     * @param   w       the value to search for
     * @return          the index of the candy bar 
     */
    public static int binaryFind(ArrayList<CandyBar> list, int first, int last, double w)
    {
        return -1;
    }

    /**
     * This method adds candy bars to a list.
     *
     * @param   list    the list to add to
     */
    public static void addCandyBarsToList(ArrayList<CandyBar> list)
    {
        CandyBar kitkat = new CandyBar("KitKat", CandyBar.Color.RED, 1.5);
        list.add(kitkat);
        CandyBar grand = new CandyBar("One-hundred Grand", CandyBar.Color.RED, 1.5);
        list.add(grand);
        CandyBar crunch = new CandyBar("Crunch", CandyBar.Color.BLUE, 1.55);
        list.add(crunch);
        CandyBar hershey = new CandyBar("Hershey", CandyBar.Color.BROWN, 1.55);
        list.add(hershey);
        CandyBar krackel = new CandyBar("Krackel", CandyBar.Color.RED, 1.55);
        list.add(krackel);
        CandyBar caramello = new CandyBar("Caramello", CandyBar.Color.PURPLE, 1.6);
        list.add(caramello);
        CandyBar what = new CandyBar("Whatchamacallit", CandyBar.Color.YELLOW, 1.6);
        list.add(what);
        CandyBar almond = new CandyBar("Almond Joy", CandyBar.Color.BLUE, 1.61);
        list.add(almond);
        CandyBar goodbar = new CandyBar("Mr. Goodbar", CandyBar.Color.YELLOW, 1.75);
        list.add(goodbar);
        CandyBar twix = new CandyBar("Twix", CandyBar.Color.GOLD, 1.79);
        list.add(twix);
        CandyBar henry = new CandyBar("Oh Henry", CandyBar.Color.YELLOW, 1.8);
        list.add(henry);
        CandyBar milkyWay = new CandyBar("Milky Way", CandyBar.Color.GREEN, 1.84);
        list.add(milkyWay);
        CandyBar payDay = new CandyBar("PayDay", CandyBar.Color.ORANGE, 1.85);
        list.add(payDay);
        CandyBar snickers = new CandyBar("Snickers", CandyBar.Color.BLUE, 1.86);
        list.add(snickers);
        CandyBar butterfinger = new CandyBar("Butterfinger", CandyBar.Color.YELLOW, 1.9);
        list.add(butterfinger);
        CandyBar musketeers = new CandyBar("Three Musketeers", CandyBar.Color.SILVER, 1.92);
        list.add(musketeers);
        CandyBar reeses = new CandyBar("Reese's FastBreak", CandyBar.Color.ORANGE, 2);
        list.add(reeses);
        CandyBar babyRuth = new CandyBar("Baby Ruth", CandyBar.Color.SILVER, 2.1);
        list.add(babyRuth);
    }
}

【问题讨论】:

    标签: java arraylist methods


    【解决方案1】:

    printInfo() 方法是在 CandyBar 对象上声明的,但在这里您尝试在 String 对象上调用 printInfo()String 上不存在此方法。

    /**
     * This method searches a list to find a candy bar by name.
     *
     * @param   list    the list to search
     * @param   n       a name to find
     * @return          the index of the candy bar 
     */
    public static int findCandyBar(ArrayList<CandyBar> list, String n)
    {
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getName() == n){
                return n.printInfo(i); <-- calling n.printInfo(i) will not work as printInfo() does not exist on String
    

    您需要注意 printInfo() 的方法签名:

    • 它存在于 CandyBar,而不是 String。
    • 它不接受任何参数参数。
    • 这是一个 void 方法,即不返回对象。

    所以而不是:

    return n.printInfo(i);
    

    使用这个:

    i.printInfo();
    

    【讨论】:

    • 好的,我应该怎么做?有什么建议可以作为糖果棒对象调用
    • CandyBar 上存在 printInfo() 方法,不接受任何参数。因此,而不是: return n.printInfo(i);使用这个:return i.printInfo();
    • "你需要注意 printInfo() 的方法签名" 嗯,public void printInfo() {} 是一个 void 方法,所以你不能用它来返回像这样。
    • 哈哈好点!已更新。
    猜你喜欢
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2020-10-19
    • 2016-05-24
    • 2018-05-13
    • 2012-04-13
    相关资源
    最近更新 更多