2016701010126 2016-2071-2 《java程式設計》一至四章總結

來源:互聯網
上載者:User

標籤:程式   [1]   next   static   etc   books   for   sum   get   

        這是課堂的最後一個編程題,我課堂上這樣寫: 

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程式設計》一至四章總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.