题目:
     编写一个程序,实现设置上月,本月电表读数,显示上月,本月电表读数,计算并显示本月用电数。假设每度电的价格为1.2元,计算并显示本月电费。


方法一:
package org.java.application;
import java.util.Scanner;
public class InputAndoutputs {


// TODO Auto-generated method stub
int lastRecord;//上月用电数
int curentRecod;//本月用电数
int usedAmount;//本月用电量
double usedFee;//本月电费
//获取上月和本月的用电数
public void setRcord() {
Scanner scan=new Scanner(System.in);
System.out.print("请输入上月用电量:");//
lastRecord=scan.nextInt();
System.out.print("请输入本月用电量:");//
curentRecod=scan.nextInt();
}
//计算本月的用电量
public int calcUsedAmount() {
usedAmount=curentRecod-lastRecord;
System.out.println("本月用电量:"+usedAmount);
return usedAmount;
}
//计算本月的电费
public double calcUsedFee() {
usedFee=1.2*usedAmount;
System.out.println("本月电费"+usedFee);
return usedFee;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Card Free=new  Card ();//声明并创建card类的对象free
Free.setRcord();//通过对象free调用card类的方法setRcord()
Free.calcUsedAmount();//通过对象free调用card类的方法calcUsedAmount()
Free.calcUsedFee();//通过对象free调用card类的方法calcUsedFee()
}

}

课后作业之计算电量和电费课后作业之计算电量和电费

方法二:
package org.java.application;
import java.util.Scanner ;
public class InputAndoutput {


public static void main(String[] args) {
// TODO Auto-generated method stub
int lastrecord;//上月用电数
int curentRecord ;//本月用电数
int usedAmount ;//本月用电量
double usedFee ;//本月电费
Scanner scan=new Scanner(System.in);
System.out.print("请输入上月用电数:");
lastrecord=scan.nextInt () ;
System.out.print ("请输入本月用电数:");
curentRecord=scan.nextInt () ;
usedAmount=curentRecord- lastrecord;
System.out.println("本月用电量:"+usedAmount);
usedFee=1.2*usedAmount;
System.out.println ("本月电费:"+usedFee) ;
}


}

课后作业之计算电量和电费课后作业之计算电量和电费

相关文章:

  • 2022-01-07
  • 2022-03-06
  • 2022-01-05
  • 2021-05-16
  • 2021-09-20
  • 2021-10-21
猜你喜欢
  • 2021-10-23
  • 2021-05-23
  • 2021-11-28
  • 2021-12-28
  • 2022-01-02
  • 2022-01-07
  • 2021-12-11
相关资源
相似解决方案