以物件導向方式編寫一個計算電話費的程式

來源:互聯網
上載者:User

       標準:基本通話費是每分鐘4角錢,不足一分鐘的部分,按照1分鐘計算;長途通話費是每6秒7分錢,不足6秒的部分按照6秒計算。整個長途通話費,由上述兩項加起來。

       public class MobileFee
{
 public void calcFee(int iSeconds)
 {
  int iFee;
  //基本通話費,每分鐘4角錢
  iFee=(int)Math.ceil(iSeconds/60.0)*40; //ceil(double a):返回最小的double值,該值大於等於參數,並等於某個整數,該函數實現向上取整。
  //長途通話費,沒6秒7分錢
  iFee+=(int)Math.ceil(iSeconds/6.0)*7;//calcFee()中除法操作時,注意分母寫成的雙精確度格式,否則會被當做整除運算。
  System.out.println("通話"+iSeconds+"秒的費用是"+iFee+"分錢!");
 }
 public static void main(String args[])
 {
  MobileFee obj=new MobileFee();
  obj.calcFee(5);
  obj.calcFee(60);
  obj.calcFee(61);
  obj.calcFee(119);
 }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.