简体   繁体   English

如何记录以前的参数值并添加到新参数值?

[英]How can I record previous parameter values and add to new one?

I want to record old values of a parameter and add new parameter values upon the old ones, how can I do this in java? 我想记录参数的旧值并在旧参数值上添加新参数值,我该如何在java中执行此操作?

For example how can I add up the "10" in the auto.fillUp and the "20" in the auto.fillUp? 例如,如何在auto.fillUp中添加“10”,在auto.fillUp中添加“20”?

public class Main {
    public static void main(String[] args){
         OdometerReading auto = new OdometerReading(15);
         auto.fillUp(350, 10);
         auto.fillUp(450, 20);
         System.out.println("Miles per gallon: " + auto.calculateMPG());
    }
}

OdometerReading Class: 里程表阅读课程:

public class OdometerReading {
    public int myStartMiles;
    public int myEndMiles;
    public double myGallonsUsed;
    public int milesInterval;
    public double getMyGallonsUsedNew;

    public OdometerReading(int assignedCarMiles){
       myStartMiles = assignedCarMiles;
       System.out.println("New car odometer reading: " + myStartMiles);
    }

    public void fillUp(int milesDriven, double gallonsUsed){
        myEndMiles = milesDriven;
        myGallonsUsed = gallonsUsed;
    }

    public double calculateMPG(){
        milesInterval = myEndMiles - myStartMiles;
        double mpg = milesInterval / myGallonsUsed;
        return mpg;
    }

    public void reset(){
        myStartMiles = myEndMiles;
        myGallonsUsed = 0;
    }

} 

***Note: I am a beginner to Java & programming in general I'm sorry if this may be an "un-professional" question. ***注意:我是Java和编程的初学者我很抱歉,如果这可能是一个“非专业”的问题。

Just make the operation in the method, the state gets saved in the object variable and as long as you have a reference to the object you are good. 只需在方法中进行操作,状态就会保存在对象变量中,只要您有对象的引用即可。

public void fillUp(int milesDriven, double gallonsUsed){
    myEndMiles += milesDriven;
    myGallonsUsed += gallonsUsed;
}

Note the + operator 注意+运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何询问用户是否要为文件添加新记录? - How can I question the user if they want to add a new record for a file? 我可以在访问前一个jPanel的同时添加一个全新的实例吗? - Can I add an entirely new instance of the same jPanel while having access to the previous one? 如何在SQL的一栏中添加多个值? - How can i add multiple values into one column in sql? 如何使用休眠和JBOSS Tool在数据库中添加新记录? - How can I add new record in database using hibernate with JBOSS Tool? 如何在我的HashMap中添加多个数据库记录? - How can I add more than one database record to my HashMap? 如何基于“自定义数组列表”中的一个参数添加值? - How would I add values based on one parameter in Custom Array List? 如何在不影响较旧的参数的情况下添加新的静态参数? - How to add a new static parameter without affecting the olders one? 如何在Android中使用下一个和上一个按钮循环数组值 - How can i cycle array values with next and previous button in Android 如何通过以前的值更新Java哈希图值 - How can I update Java hashmap values by previous value 如何在 webview 片段上添加回上一页? - How can I add back to previous page on webview fragments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM