首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >印刷方法

印刷方法
EN

Stack Overflow用户
提问于 2014-07-23 12:31:06
回答 7查看 107关注 0票数 0

我试图打印一个方法的内容(购买(字符串isbn,双价,int副本),但没有运气。在编写以下代码时,

代码语言:javascript
复制
import java.util.Scanner; 
import java.io.*; 

public class Store { 
    public static void main(String[] args) throws Exception {
    Book[] books = readInventory();

        for (Book book : books) {
            System.out.printf("ISBN: %s, Price: %f, Copies: %d%n", book.getISBN(), book.getPrice(), book.getCopies()); 
        }
    String isbn;
    double price;
    int copies;
    purchase(isbn, price, copies);

    }

    public static Book[] readInventory() throws Exception {
        Book[] books = new Book[10];
        java.io.File file = new java.io.File("../instr/prog4.dat");
        Scanner fin = new Scanner(file);
        String isbn;
        double price;
        int copies;
        int i = 0;

        while (fin.hasNext()) {
            isbn = fin.next();
                if (fin.hasNextDouble()); {
                    price = fin.nextDouble();
                }
                if (fin.hasNextInt()); {
                    copies = fin.nextInt();
                }
             Book book = new Book(isbn, price, copies);
             books[i] = book;
             i++;
        }
        fin.close();
        return books;
 }

    public static Book[] purchase(String isbn, double price, int copies, Book[] books) {
        int itemsSold = 0;
        double totalMade = 0;
        Scanner input = new Scanner(System.in);
        int desiredCopies = 0;

        System.out.println("Please enter the ISBN number of the book you would like to purchase: ");
            String desiredIsbn = input.next();
            for(int index = 0; index < books.length; index++) {
                if(!books[index].getISBN().equals(desiredIsbn))
                    System.out.println("We do not have that book in our inventory.");
                if(books[index].getISBN().equals(desiredIsbn) && copies == 0)
                    System.out.println("That book is currently out of stock.");
                if(books[index].getISBN().equals(desiredIsbn) && copies > 0) {
                    System.out.println("How many copies of this book would you like to purchase?"); 
                        desiredCopies = input.nextInt(); }
                        if(desiredCopies > copies)
                            System.out.println("We only have " + copies + "in stock. Please select another quantity: ");
                            desiredCopies = input.nextInt();
                        // copies = copies - desiredCopies
                        double total = price * desiredCopies;
                    System.out.println("Thank you for your purchase, your order total is: $" + total);
                    itemsSold += desiredCopies;
                    totalMade += total;
                    // update array
                    System.out.print(books[index]);
                    System.out.println("We sold " + itemsSold + " today.");
                    System.out.println("We made $" + totalMade + "today.");
            }   
        return books;
        }

    public void displayInfo(Book[] books) {
        for(int x=0; x<books.length; x++) {
             System.out.println("ISBN: " + books[x].getISBN() + "\n Price: " +
                books[x].getPrice() + "\n Copies: " + books[x].getCopies());
        System.out.print(books[x]);
        }
    }
}

class Book {
 private String isbn;
 private double price;
 private int copies;

 public Book() {
 }

 public Book(String isbnNum, double priceOfBook, int copiesInStock) {
  isbn = isbnNum;
  price = priceOfBook; 
  copies = copiesInStock;
 }

 public String getISBN() {
  return isbn;
 }

 public double getPrice() {
  return price;
 }

 public int getCopies() {
  return copies;
 }

 public void setISBN(String isbn) {
  this.isbn = isbn;
 }

 public void setPrice(double price) {
  this.price = price;
 }

 public void setCopies(int copies) {
  this.copies = copies;
 }

   @Override
    public String toString() {
        return String.format("ISBN: %s, Price: %f, Copies: %d%n",
            this.getISBN(), this.getPrice(), this.getCopies());
    }

}

我得到编译器错误

代码语言:javascript
复制
Store.java:21: purchase(java.lang.String,double,int,Book[]) in Store cannot be applied to (java.lang.String,double,int)
        purchase(isbn, price, copies);
        ^
1 error

如果我评论一下:

代码语言:javascript
复制
String isbn;
double price;
int copies;
purchase(isbn, price, copies);

main()方法的一部分,程序打印数组,但没有其他任何内容。我需要程序打印的购买方法,包括更新的数组(我仍然不知道如何这样做,任何帮助,对此将不胜感激)。

关于我怎样才能让这件事奏效,有什么建议吗?如果可以的话,我想尽可能接近我编写的代码,过去几天我一直在做这个工作,但是大约一个半小时后就要到期了,所以我已经没有时间了。提前谢谢。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-07-23 12:33:55

您的purchase方法使用与您调用的签名不同的签名。

purchase需要String, double, int, Book[]作为参数,但是您正在尝试用String, double, int调用它。尝试添加一个图书阵列。

最重要的是,纵观purchase,看起来大多数参数甚至都没有被使用。您应该考虑使用这些参数或删除它们。

PS:您可能需要考虑使用像Eclipse、Netbeans或IntelliJ这样的IDE来帮助捕获这些常见的错误:)

票数 1
EN

Stack Overflow用户

发布于 2014-07-23 12:33:40

编译器错误是因为您传递了三个参数,而该方法需要四个参数。以下代码将编译:

代码语言:javascript
复制
purchase(isbn, price, copies, books);

但是,您的代码在其他方面看上去是错误的,因为您没有为isbnpricecopies分配任何值。Book类已经包含这些值,因此只需要向打印方法提供一个Book对象数组。

例如,将购买方法更改为:

代码语言:javascript
复制
public static Book[] purchase(Book[] books) {
  int itemsSold = 0;
  double totalMade = 0;
  Scanner input = new Scanner(System.in);
  int desiredCopies = 0;

  System.out
      .println("Please enter the ISBN number of the book you would like to purchase: ");
  String desiredIsbn = input.next();
  for (int index = 0; index < books.length; index++) {
    if (!books[index].getISBN().equals(desiredIsbn))
      System.out.println("We do not have that book in our inventory.");
    if (books[index].getISBN().equals(desiredIsbn) && books[index].getCopies() == 0)
      System.out.println("That book is currently out of stock.");
    if (books[index].getISBN().equals(desiredIsbn) && books[index].getCopies() > 0) {
      System.out
          .println("How many copies of this book would you like to purchase?");
      desiredCopies = input.nextInt();
    }
    if (desiredCopies > books[index].getCopies())
      System.out.println("We only have " + books[index].getCopies()
          + "in stock. Please select another quantity: ");
    desiredCopies = input.nextInt();
    // copies = copies - desiredCopies
    double total = books[index].getPrice() * desiredCopies;
    System.out.println("Thank you for your purchase, your order total is: $"
        + total);
    itemsSold += desiredCopies;
    totalMade += total;
    // update array
    System.out.print(books[index]);
    System.out.println("We sold " + itemsSold + " today.");
    System.out.println("We made $" + totalMade + "today.");
  }
  return books;
}

其他一些注意事项:

  • 尝试一个for-每一个循环,例如for (Book book : books) {
  • 考虑使用货币类型而不是double
  • 不要在需要变量之前声明变量。
票数 3
EN

Stack Overflow用户

发布于 2014-07-23 12:34:15

您在purchase()方法中传递了错误的参数数。你已经宣布了

public Book[] purchase(String isbn, double price, int copies, Book[] books)//taking four arguments

即有四个参数,但调用它时只传递三个参数,即

purchase(isbn, price, copies);//but you are calling it by passing only three parameters reason for the error

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24910541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档