JAVA作业11-1

来源:互联网 发布:创维网络机顶盒 编辑:程序博客网 时间:2024/06/05 14:54

JAVA源码:

import java.util.Scanner;//引入Scanner类,负责从键盘接收数据

public class Power {
double lastRecord;
double currentRecord;
double usedAmount;
double usedFee;

public void setRecord(){
Scanner scan=new Scanner(System.in);
System.out.print("请输入上月用电数:");
lastRecord=scan.nextInt();//读入一个字符串
System.out.printf("请输入本月用电数:");
currentRecord=scan.nextInt();//读入一个int型整数
//System.out.printf("已经输入上月用电数s=%s\n",lastRecord);
//System.out.printf("已经输入本月用电数i=%d\n",currentRecord);
}


public void showRecord(){
System.out.printf("\n所有输入的数据:\n上月用电数:"+lastRecord+"\n本月用电数:"+currentRecord);
}


public double calcUsedAmount(){
usedAmount=currentRecord-lastRecord;
System.out.println("本月用电数"+usedAmount);
return usedAmount;
}


public void calcUsedFee(){
usedFee=1.2*usedAmount;
System.out.println("本月用电费"+usedFee);


}


public static void main(String[] args) {
Power du=new Power();
du.setRecord();
du.showRecord();
du.calcUsedAmount();
du.calcUsedFee();

}

}


运行结果:






原创粉丝点击