新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Scanner input=new Scanner(System.in);
创新互联专注于中大型企业的网站制作、成都网站制作和网站改版、网站营销服务,追求商业策划与数据分析、创意艺术与技术开发的融合,累计客户上千多家,服务满意度达97%。帮助广大客户顺利对接上互联网浪潮,准确优选出符合自己需要的互联网运用,我们将一直专注品牌网站建设和互联网程序开发,在前进的路上,与客户一起成长!
System.out.println("请输入本月用电量:");
int num=0;
try {
num=input.nextInt();
} catch (Exception e) {
System.out.println("请输入数字");
}
double count=0;
if(num200){
count=num*0.10;
}else if(num=200 num=500){
count=num*0.30;
}else{
count=num*0.50;
}
System.out.println("你需交电费:"+count);
import java.util.Date;
public class Test {
public static void main(String[] args){
System.out.println("夏季用125度电的总电费是:"+new DianFei(125,true).getFee());
System.out.println("冬季用125度电的总电费是:"+new DianFei(125,false).getFee());
}
}
//电费类
class DianFei {
private double data;
private boolean b;
private double dj=0.49;//每度电费的价格
//data 为使用的度数,b为true表示夏天,b为false表示冬天
public DianFei(double data,boolean b){
this.data=data;
this.b=b;
}
//如果只传入一个用电度数,那么4-9月为夏,其他为冬
public DianFei(double data){
this.data=data;
int month=new Date().getMonth();
if(month=3 month=8){//月数十从0开始的
this.b=true;
}else{
this.b=false;
}
}
//得到电费的钱数
public double getFee(){
if(!b) data+=20;
return data*dj;
}
}
接口和类全在一个包下,如果不在一个包下记得import调用,show的重写你根据需要自己改
//ShouFei 接口
public interface ShouFei {
public void fuFei(double mileage);
public void show();
}
//Bus类
public class Bus implements ShouFei{
String airConditioning;
double money;
public Bus(String airConditioning){
this.airConditioning = airConditioning;
}
@Override
public void fuFei(double mileage) {
if(airConditioning.equals("空调")){
money = 2;
}else{
money = 1;
}
}
@Override
public void show() {
System.out.println("公交车,"+airConditioning+",费用:"+money);
}
}
//Taxi类
public class Taxi implements ShouFei{
double mileage;
boolean isDay;
double money;
public Taxi(boolean isDay){
this.isDay = isDay;
}
@Override
public void fuFei(double mileage) {
this.mileage = mileage;
//判断是否为白天
if(isDay){
//判断里程数是否大于三公里
if(mileage3){
money=8;
}else{
money = 8 + (mileage-3)*1.5;
}
}else{
if(mileage3){
money=10;
}else{
money = 10 + (mileage-3)*1.5;
}
}
}
@Override
public void show() {
System.out.print("出租车,");
if(isDay){
System.out.print("白天");
}else{
System.out.print("晚上");
}
System.out.println(",里程数:"+mileage+"收费:"+money);
}
}
//测试类
public class Test {
public static void main(String[] args) {
Bus bus = new Bus("空调");
bus.fuFei(10);
bus.show();
Taxi taxi = new Taxi(true);
taxi.fuFei(20);
taxi.show();
}
}