我正在上一个基础Java课,并且我正在完成作业。我唯一的问题是弄清楚我的货币格式。我没有得到正确的转换和输出。按照我的作业,我有两个文件,一个类和一个可执行文件,在价格降低40%之后,我必须显示图书的价格,并显示折扣后最昂贵的图书。我不确定如何确定正确输出的货币格式。
这是我的程序应该显示的内容。
这是我的程序显示的内容
Book title:Java Programming, author:Liang, pages:1320, price: $145.0
Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99
Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25
Book title:Born a Crime, author:Noah, pages:304, price: $17.33
Book title:null, author:null, pages:0, price: $0.0
Book title:null, author:null, pages:0, price: $0.0
Books after completing library and 40% discount
Book title:Java Programming, author:Liang, pages:1320, price: $58.0
Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $7.9959999999999996
Book title:The Hobbit, author:Tolkien, pages:320, price: $3.7
Book title:Born a Crime, author:Noah, pages:304, price: $6.9319999999999995
Book title:Dark Territory, author:Kaplan, pages:352, price: $4.496
Book title:Born to Run, author:Springsteen, pages:508, price: $4.868
Here is the most expensive book after discounts
Book title:Java Programming, author:Liang, pages:1320, price: $58.0
这是我的代码:这是课程文件
public class Book {
//instance data members
private String title;
private String author;
private int pages;
private double price;
//public static int set to 0
public static int numBooks = 0;
//parameterized constructor
Book(String title, String author, int pages, double price){
this.title=title;
this.author=author;
this.pages=pages;
this.price=price;
numBooks++;
}
//default constructor
Book(){
title = author = null;
price = pages = 0;
numBooks++;
}
//getters and setters
void setTitle(String title) {
this.title=title;
}
void setAuthor(String author) {
this.author=author;
}
void setPages(int pages) {
this.pages=pages;
}
void setPrice(double price) {
this.price=price;
}
String getTitle() {
return title;
}
String getAuthor() {
return author;
}
int getPages() {
return pages;
}
double getPrice() {
return price;
}
//toString method to return string showing state of a book instance
@Override
public String toString() {
String str = "Book title:" + title + ", author:" + author +
", pages:" + pages + ", price: $" + price;
return str;
}
}
这是可执行文件
public class TestBook {
public static void main(String[] args) {
// array to hold 6 book objects specify data in first 4
Book[] bookArray=new Book[6];
bookArray[0]=new Book("Java Programming","Liang",1320,145.00);
bookArray[1]=new Book("Horton Hears a Who!","Dr.Seuss",72,19.99);
bookArray[2]=new Book("The Hobbit","Tolkien",320,9.25);
bookArray[3]=new Book("Born a Crime","Noah",304,17.33);
bookArray[4]=new Book();
bookArray[5]=new Book();
for(Book b:bookArray)
System.out.println(b.toString());
System.out.println("\nBooks after completing library and 40% discount");
finishArray(bookArray);
Book expensive = reduceBooks(bookArray);
for(Book b:bookArray)
System.out.println(b.toString());
System.out.println("\nHere is the most expensive book after discounts\n" + expensive.toString());
}
//void method, use setter method to fill in last 2 books in array
static void finishArray(Book[]array) {
array[4].setTitle("Dark Territory");
array[4].setAuthor("Kaplan");
array[4].setPages(352);
array[4].setPrice(11.24);
array[5].setTitle("Born to Run");
array[5].setAuthor("Springsteen");
array[5].setPages(508);
array[5].setPrice(12.17);
}
static Book reduceBooks(Book[]array) {
Book mostExpensiveBook=null;
//reduce price of every book by 40%
for (int i=0; i<6; i++) {
array[i].setPrice(array[i].getPrice()*0.40);
if(mostExpensiveBook==null)
mostExpensiveBook=array[i];
else if(mostExpensiveBook.getPrice()<array[i].getPrice())
mostExpensiveBook = array[i];
}
return mostExpensiveBook;
}
}
答案 0 :(得分:0)
“一个”简单的解决方案可能是使用NumberFormat
类,例如...
public static class Book {
//instance data members
protected static final NumberFormat FORMATTER = NumberFormat.getCurrencyInstance();
//...
@Override
public String toString() {
String str = "Book title:" + title + ", author:" + author
+ ", pages:" + pages + ", price: " + FORMATTER.format(price);
return str;
}
}
哪个会输出类似...
Book title:Java Programming, author:Liang, pages:1320, price: $145.00
Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99
Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25
Book title:Born a Crime, author:Noah, pages:304, price: $17.33
Book title:null, author:null, pages:0, price: $0.00
Book title:null, author:null, pages:0, price: $0.00
Books after completing library and 40% discount
Book title:Java Programming, author:Liang, pages:1320, price: $58.00
Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $8.00
Book title:The Hobbit, author:Tolkien, pages:320, price: $3.70
Book title:Born a Crime, author:Noah, pages:304, price: $6.93
Book title:Dark Territory, author:Kaplan, pages:352, price: $4.50
Book title:Born to Run, author:Springsteen, pages:508, price: $4.87
Here is the most expensive book after discounts
Book title:Java Programming, author:Liang, pages:1320, price: $58.00
您可以配置格式化程序以更好地满足您的个人要求,但这应该为您提供一个起点
“替代”解决方案可能是利用String.format
,例如...
@Override
public String toString() {
String str = "Book title:" + title + ", author:" + author
+ ", pages:" + pages + ", price: " + String.format("$%.2f", price);
return str;
}
会执行类似的工作(但是NumberFormat
在格式化Locale
时会考虑当前用户String
)