zoukankan      html  css  js  c++  java
  • java,编写一个汽车类

    package org.hanqi.pn0120;
    
    //汽车
    public class Car {
        // 颜色
        private String yanse;
        // 品牌
        private String pinpai;
        // 油箱容量
        private double youxiangrl = 40;
        // 当前油量
        private double dangqianyl = 0;
    
        public double getdangqianyl() {
            return this.dangqianyl;
        }
    
        // 加油
        public void jiaYou(double youliang) {
            if (youliang < 0) {
                System.out.println("不要偷我的油!");
            } else if (this.dangqianyl + youliang > this.youxiangrl) {
                // 本次加油量
                double bcyl = this.youxiangrl - this.dangqianyl;
                this.dangqianyl = this.youxiangrl;
                System.out.println("油太多了,已经加满了,这次加了" + (bcyl) + "");
            } else {
                this.dangqianyl += youliang;
                System.out.println("这次加了" + youliang + "升油");
            }
    
        }
    
        // private double youhao=
        private double xingshizlc = 0;
    
        public double getXingshizlc() {
            return this.xingshizlc;
        }
    
        // 行驶
        public void xingshi(double xingshilc, double youhao) {
            if (this.dangqianyl <= 0) {
                System.out.println("车没有油,怎么开车");
                return;
            }
            if (xingshilc < 0 || youhao < 0) {
                System.out.println("不可能这么开车");
            } else {
    
            }
            // 本次油耗
            double dangqyh = xingshilc * youhao / 100;
            this.dangqianyl -= dangqyh;
            // this.dangqianyl = this.dangqianyl > 0 ? this.dangqianyl : 0;
            if (this.dangqianyl < 0) {
                this.dangqianyl = 0;
                System.out.println("你是推着车走么");
            }
            this.xingshizlc += xingshilc;
            System.out.println("本次行驶了" + xingshilc + "公里");
        }
    
        public String getYanse() {
            return yanse;
        }
    
        public void setYanse(String yanse) {
            this.yanse = yanse;
        }
    
        public String getPinpai() {
            return pinpai;
        }
    
        public void setPinpai(String pinpai) {
            this.pinpai = pinpai;
        }
    
        public double getYouxiangrl() {
            return youxiangrl;
        }
    
        public void setYouxiangrl(double youxiangrl) {
            this.youxiangrl = youxiangrl;
        }
    
        public Car(String pinpai, double youxiangrl) {
            super();
            this.pinpai = pinpai;
            this.youxiangrl = youxiangrl;
        }
    
        // main方法
        public static void main(String[] args) {
            Car myBenchi = new Car("奔驰", 50);
            System.out.println("品牌=" + myBenchi.getPinpai());
            System.out.println("油箱容量=" + myBenchi.getYouxiangrl());
            myBenchi.xingshi(200, 13);
            myBenchi.jiaYou(30);
            myBenchi.jiaYou(30);
            System.out.println("当前容量=" + myBenchi.getdangqianyl());
            myBenchi.xingshi(300, 15);
            System.out.println("当前容量=" + myBenchi.getdangqianyl());
            myBenchi.xingshi(200, 13);
            System.out.println("行驶总里程=" + myBenchi.getXingshizlc());
            System.out.println("当前容量=" + myBenchi.getdangqianyl());
    
        }
    
    }

  • 相关阅读:
    【RS】Automatic recommendation technology for learning resources with convolutional neural network
    卷积神经网络的入门
    Debug 路漫漫-10:AttributeError: 'Embedding' object has no attribute 'get_shape'
    Debug 路漫漫-09:构建CNN时维度不一致问题
    Debug 路漫漫-08:Keras 版本升级函数变换导致的问题
    springboot 过滤器、拦截器、消息转换器、切片执行顺序 及区别
    java InputStream读取数据问题
    Springboot 2-OAuth 2修改登录加密方式
    Spring Boot Security Oauth2之客户端模式及密码模式实现
    oauth2.0通过JdbcClientDetailsService从数据库读取相应的配置
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5245073.html
Copyright © 2011-2022 走看看