编写一个程序,实现设置上月、本月电表读数,显示上月、本月电表读数,计算并显示本月用电数。

该JAVA程序用于设置并显示上月和本月电表读数,计算本月用电量和电费。用户输入上月和本月读数,程序通过计算差值确定用电量,并按照1.2元/度的价格计算电费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 编写一个程序,实现设置上月、本月电表读数,显示上月、本月电表读数,计算并显示本月用电数。
 *假设每度电的价格为1.2元,计算并显示本月电费
 */
package JAVA;
import java.util.Scanner;//引Scanner入类
class java {// 创建电这个类
double lastRecord;// 成员变量上月用电数
double currentRecord;// 成员变量本月用电数
double usedAmount;// 成员变量本月用电数
double usedFee;// 成员变量本月用电费
public void setRecord() {// 用方法setRecord()从键盘上输入上月用电数和本月用电数
System.out.print("请输入上月用电数:");
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
lastRecord = scan.nextInt();
这是一个简单的电力计费系统模拟程序,我们将创建一个名为`ElectricMeter`的类来实现所需功能。首先,我们需要一些基本属性来存储上月本月读数以及电价信息。假设电价固定不变: ```java public class ElectricMeter { private double lastMonthReading; private double thisMonthReading; private double electricityRatePerUnit; // 构造函 public ElectricMeter(double rate) { electricityRatePerUnit = rate; } // 设置上月读数 public void setLastMonthReading(double reading) { lastMonthReading = reading; } // 设置本月读数 public void setThisMonthReading(double reading) { thisMonthReading = reading; } // 显示上月读数 public double getLastMonthReading() { return lastMonthReading; } // 显示本月读数 public double getThisMonthReading() { return thisMonthReading; } // 计算本月用电量 public double calculateMonthlyUsage() { return Math.abs(thisMonthReading - lastMonthReading); } // 计算本月电费 public double calculateMonthlyCost() { double usage = calculateMonthlyUsage(); if (usage > 0) { // 确保有用电量才计算费用 return usage * electricityRatePerUnit; } else { return 0; // 没有用电,费用为零 } } // 显示本月电费 public String displayMonthlyCost() { double cost = calculateMonthlyCost(); return "本月电费: " + cost; } } // 主方法演示如何使用 public static void main(String[] args) { ElectricMeter meter = new ElectricMeter(0.15); // 假设电价每度0.15元 meter.setLastMonthReading(1000); // 上月读数 meter.setThisMonthReading(1200); // 本月读数 System.out.println("上月读数: " + meter.getLastMonthReading()); System.out.println("本月读数: " + meter.getThisMonthReading()); System.out.println("本月用电量: " + meter.calculateMonthlyUsage()); System.out.println(meter.displayMonthlyCost()); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值