题目:
编写一个程序,实现设置上月,本月电表读数,显示上月,本月电表读数,计算并显示本月用电数。假设每度电的价格为1.2元,计算并显示本月电费。
方法一:
package com.task01;
import java.util.Scanner;
public class Card {
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;
}
import java.util.Scanner;
public class Card {
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()
}
// 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 com.task01;
import java.util.Scanner ;
public class InputAndoutput {
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) ;
// 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) ;
}
}