我正在尝试打印方法的内容(purchase(String isbn, doubleprice, int Copy)),但我没有运气。如以下代码所示,
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());
}
}
我收到编译器错误
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
如果我注释掉:
String isbn;
double price;
int copies;
purchase(isbn, price, copies);
main() 方法的一部分,程序打印数组,但不打印其他内容。我需要程序来打印购买方法,包括更新的数组(我仍然不知道如何执行此操作,因此也将不胜感激)。
关于如何让它发挥作用有什么建议吗?如果可以的话,我想尽可能贴近我编写的代码,过去几天我一直在研究这个问题,但大约一个半小时后就要到期,所以我的时间不多了。提前致谢。
最佳答案
编译器错误是因为您传递了三个参数,而该方法需要四个参数。以下代码将编译:
purchase(isbn, price, copies, books);
但是,您的代码在其他方面看起来是错误的,因为您没有为 isbn
、price
或 copies 分配任何值
。您的 Book
类已包含这些值,因此您只需向打印方法提供 Book
对象数组即可。
例如将您的购买方式更改为:
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-each 循环,例如
for(书籍书籍:书籍){
- 考虑使用货币类型而不是
double
。 - 不要在需要变量之前声明它们。
关于java - 打印方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24910541/