这是课堂的最后一个编程题,我课堂上这样写:
package book;
public class Book {
public static void main(String[] args)
{
Books[] books=new Books[3];
books[0]=new Books("Java程序设计",119.00);
books[1]=new Books("C语言程序设计",72.50);
books[2]=new Books("Python程序设计",35.85);
for(Books e:books){
e.setId();
System.out.println("书名:"+e.getName()+",书号:"+e.getId()
+",书价:"+e.getPrice());
}
int n=Books.getNextID();
System.out.println(n);
}
}
package books;
public class Books {
private static int nextId=0;
private String name;
private int id;
private double price;
public Books(String n,double p)
{
name=n;
id=0;
price=p;
}
public String getName(){
return name;
}
public int getId(){
return id;
}
public double getPrice(){
return price;
}
public void setId(){
nextId++;
id=nextId;
}
public static int getNextID(){
return nextID;
}
}
接下来是我修改后的,我觉得这个还行,请帮忙检查修改一下
package book;
public class Book {
public static void main(String[] args)
{
Books[] books=new Books[3];
books[0]=new Books("Java程序设计",119.00f,3);
//实数默认为double,float型实数后应该加f/F;
books[1]=new Books("C语言程序设计",72.50f,8);
books[2]=new Books("Python程序设计",35.85f,5);
for(Books e:books){
e.setId();
System.out.println("书名:"+e.getName()+",书号:"+e.getId()
+",书价:"+e.getPrice()+",书册:"+e.getCount());
}
int n=Books.getSumcount();
System.out.println("图书总数:"+n);
}
}
package book;
public class Books {
private static int nextId=0;
private String name;
private int id;
private float price;
private int count;
private static int sumcount=0;
public Books(String name,float price,int count)
//(String n,double p)
{
/*name=n;
id=0;
price=p;*/
this.name=name;
this.id=0;
this.price=price;
this.count=count;
}
public String getName(){
return name;
}
public int getId(){
return id;
}
public float getPrice(){
return price;
}
public int getCount(){
return count;
}
public void setId(){
nextId++;
id=nextId;
sumcount+=id*count;
}
public static int getSumcount(){
return sumcount;
}
}
通过这次测验,我知道了我自认为对前四章很了解的内容还是不熟悉,我应该在学习后面的课程的同时复习前面的知识,不能觉得自己已经掌握了就不看了。