当前位置: 首页 > 知识库问答 >
问题:

如何修复“找不到符号”错误?

丘畅
2023-03-14

我在我的BookTest类上收到三条错误消息,它说在getCode()getCategorycalculateAX上找不到符号。我该如何解决这个问题?我试图在对话框中打印出来,但这三个是唯一不起作用的。

import javax.swing. JOptionPane;

public class BookTest
{
    public static void main(String args[])
    {
        double charge;
        double grandTotal= 0;

        String dataArray[][] = {{"NonFiction", "Abraham Lincoln Vampire Hunter","Grahame-Smith","978-0446563079","13.99","Haper","NY","US","Political"},
                {"NonFiction", "Frankenstein","Shelley","978-0486282114","7.99","Pearson", "TX","England", "Historical"},
                {"Fiction", "Dracula","Stoker","978-0486411095","5.99","Double Day", "CA","4918362"},
                {"NonFiction", "Curse of the Wolfman"," Hageman","B00381AKHG","10.59","Harper", "NY","Transylvania","Historical"},
                {"Fiction", "The Mummy","Rice","978-0345369949","7.99","Nelson","GA","3879158"}};

        Book bookArray[] = new Book[dataArray.length];

        int quantityArray[] = {12, 3, 7, 23, 5};

        for (int i = 0; i < dataArray.length; i++)
        {
            if (dataArray[i][0] == "NonFiction")
            {
                bookArray[i] = new NonFictionBook(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][4]),
                        new Publisher(dataArray[i][5], dataArray[i][6]), dataArray[i][7], dataArray[i][8]);
            }
            else
            {
                bookArray[i] = new FictionBook(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][4]),
                        new Publisher(dataArray[i][5], dataArray[i][6]), Integer.parseInt(dataArray[i][7]));
            }
        }
        String msg = "";
        for (int i = 0; i < bookArray.length; i++)
       {
            charge = bookArray[i].calculateTotal(quantityArray[i]);
            grandTotal = charge + grandTotal;
            msg += String.format("%s  %s  %d  $%.2f  $%.2f\n", bookArray[i].getTitle(), bookArray[i].getCategory(), bookArray[i].getCode(), bookArray[i].calculateTax, charge);  //this is where i get the 3 error messages. im trying to print all in one dialog box the title, category of the book, charge and tax for each book.  
        }
        msg += String.format("Grand Total  $%.2f ",  grandTotal);
        JOptionPane.showMessageDialog(null, msg);
    }
}

**************************************************************
public abstract class Book implements Tax
{
    private String title;
    private String author;
    private String isbn;
    private Double price;
    private Publisher publisher;

    public Book()
    {
        setTitle("");
        setAuthor("");
        setIsbn("");
        setPrice(0.0);
        setPublisher(new Publisher());
    }

    public Book(String t, String a, String i, double p, Publisher n)
    {

        setTitle(t);
        setAuthor(a);
        setIsbn(i);
        setPrice(p);
        setPublisher(n);
    }

    public void setTitle(String t)
    {
        title = t;
    }

    public String getTitle()
    {
        return title;
    }

    public void setAuthor(String a)
    {
        author = a;
    }

    public String getAuthor()
    {
        return author;
    }

    public void setIsbn(String i)
    {
        isbn = i;
    }

    public String getIsbn()
    {
        return isbn;
    }

    public void setPrice(double p)
    {
        price = p;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPublisher(Publisher n)
    {
        publisher = n;
    }

    public Publisher getPublisher()
    {
        return publisher;
    }

    public abstract double calculateTotal(int quantity);


    public double calculateTax(double a)
    {
        return a * .08;
    }

    public String toString()
    {
        return( " Title " + title + " Author " + author + " Isbn " + isbn
                + " Price " + price + " Publisher " + publisher.toString());
    }
}

********************************************************

public class NonFictionBook extends Book
{
    private String country;
    private String category;

    public NonFictionBook()
    {
        super();
        setCountry("");
        setCategory("");
    }

    public NonFictionBook(String t, String a, String i, double p, Publisher n, String c, String ca)
    {
        super(t,a,i,p,n);
        setCountry(c);
        setCategory(ca);
    }

    public void setCountry(String c)
    {
        country = c;
    }

    public void setCategory(String ca)
    {
        category = ca;
    }

    public String getCountry()
    {
        return country;
    }

    public String getCategory()
    {
        return category;
    }

    public String toStirng()
    {
        return( super.toString() + "Country " + country + " Category " + category);
    }

    public double calculateTotal(int quantity)
    {
        double charge =0;
        charge = (quantity * getPrice());

        if( country != "US" )
            charge += 50;

        return charge;
    }

}
*********************************************
public class FictionBook extends Book
{
    private int code;
    public FictionBook()
    {
        super();
        setCode(0);
    }

    public FictionBook(String t, String a, String i, double p, Publisher n, int c)
    {
        super(t,a,i,p,n);
        setCode(c);
    }

    public void setCode(int c)
    {
        code = c;
    }

    public int getCode()
    {
        return code;
    }

    public String toString()
    {
        return (super.toString() + " Code " + code);
    }

    public double calculateTotal(int quantity)
    {
        double charge =0;

        charge = (quantity * getPrice());

        if (quantity > 5)
            charge += 5 * (quantity - 5);

        return charge;
    }
}

共有3个答案

杨骏
2023-03-14

您的阵列使用类型手册:

Book bookArray[] = new Book[dataArray.length];

稍后,将子类添加到此数组中,如NonFictionBook。由于code类别等只是子类的一部分,因此无法使用Book数组引用访问它们。

// won't work because class Book does not have these methods, properties
bookArray[i].getCategory(), bookArray[i].getCode(), bookArray[i].calculateTax

您需要转换对象(基于数组中的类型)。

谈阎宝
2023-03-14

在得到错误的行中,您尝试打印不是Book类实例而是其子类实例的字段。你有一系列的书,你反复阅读,并试图打印信息。但并不是所有的书都有getCode()方法(只有小说类的书),只有非小说类的书有getCategory()方法。所以你不能对所有的书都这么做。

您可以根据书籍类型更改打印,或者您可以在Book类中创建一个方法,每个子类都可以覆盖它,打印您为该类所拥有的信息(如toString方法)。然后在主类中使用它。

也正如注释测试中指出的,如果字符串与等于运算符相等,而不是==或=

穆建华
2023-03-14

树方法在Books的子类中实现。因此,您必须将值转换为子类。

if (bookArray[i] instanceof FictionBook){
   FictionBook fb = (FictionBook)bookArray[i];
       msg += String.format("%s  %s  %d  $%.2f  $%.2f\n", fb.getTitle(), "", fb.getCode(), 0, charge);  
}

if (bookArray[i] instanceof NonFictionBook){
   NonFictionBook fb = (NonFictionBook)bookArray[i];
        msg += String.format("%s  %s  %d  $%.2f  $%.2f\n", nfb.getTitle(), nfb.getCategory(), nfb.getCode(), nfb.calculateTax, charge);
}

等等

您还必须使用equals()来比较字符串。非==

 类似资料:
  • 尝试以以下方式创建(或者更确切地说是学习): 我正在使用一个在线编译器,并且已经搜索了很多,我发现我的声明方式是正确的,但是其他的东西正在弹出错误 下面是错误 我需要的帮助是:我只是试图获得创建一个hashmap的基础,并在其中插入一些键和值,但上面的错误阻止了我在第一步............................................................:)

  • 我有Xcode6.2测试版。正在尝试 在中产生 Xcrun:错误:找不到实用工具“simctl”,不是开发人员工具或在路径中

  • 每次运行代码时,我都会得到相同的错误:

  • 我刚从最近买的一本书开始学习编程,它教我用java为minecraft编写插件。第一个任务是复制下面的代码,当我进入minecraft世界时,它应该说“你好” 但是当我试图编译代码时,它显示以下错误: 正如我所说,我还不知道编程,如果有人能告诉我错误是什么,我会很高兴。

  • 这里有一个类似的答案:如何在Java中将函数作为参数传递? 但提供的正确答案不起作用。我有一门课: 在函数内部我试图将传递到,但我得到的错误是: 找不到符号 符号:类Callable 我不知道为什么。 另外,我尝试使用返回类型字符串作为xMethod,您能传递一个返回类型不同的函数吗?

  • 我还使用了这个实现 我单击了“文件”菜单上的“无效缓存并重新启动”命令,然后重新生成项目,但仍然出现错误 事先谢谢你