用Java编写一个Book类,有name,pages两个属性.在构造函数中初始化两个属性值。使用循环十次,在一个HashMap
对象中放入十本书,以编号为键,Book对象为值。编号分别为SA01,SA02.....SA10;书名分别为softa,softb,....softj;页数为1,3,5,....
对象中放入十本书,以编号为键,Book对象为值。编号分别为SA01,SA02.....SA10;
书名分别为softa,softb,....softj;页数为1,3,5,....最后输出编号为SA08的书名和页数。
谢谢求解!我将不胜感激! 展开
书名分别为softa,softb,....softj;页数为1,3,5,....最后输出编号为SA08的书名和页数。
谢谢求解!我将不胜感激! 展开
1个回答
展开全部
public class Book {
private String name;
private int pages;
public Book(String name, int pages) {
this.name = name;
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public static void main(String[] args) {
char c ;
String code;
Map<String,Book> map = new HashMap<String,Book>();
for(int i=1;i<=10;i++){
c = (char)(96+i);
Book book = new Book("soft"+c,i*2-1);
if(i<10){
code="SA0"+i;
}else{
code = "SA"+i;
}
map.put(code,book);
}
Book book = map.get("SA08");
System.out.println(book.getName());
System.out.println(book.getPages());
}
}
private String name;
private int pages;
public Book(String name, int pages) {
this.name = name;
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public static void main(String[] args) {
char c ;
String code;
Map<String,Book> map = new HashMap<String,Book>();
for(int i=1;i<=10;i++){
c = (char)(96+i);
Book book = new Book("soft"+c,i*2-1);
if(i<10){
code="SA0"+i;
}else{
code = "SA"+i;
}
map.put(code,book);
}
Book book = map.get("SA08");
System.out.println(book.getName());
System.out.println(book.getPages());
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询