2016701010126 2016-2071-2 《java程序設計》一至四章總結
這是課堂的最後一個編程題,我課堂上這樣寫:
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;
}
}
通過這次測驗,我知道了我自認為對前四章很了解的內容還是不熟悉,我應該在學習後面的課程的同時復習前面的知識,不能覺得自己已經掌握了就不看了。
2016701010126 2016-2071-2 《java程序設計》一至四章總結