簡體   English   中英

java-如何-運行不在主要功能中的總計

[英]java - How to - Running total that is not in main function

我需要保持運行總計,但是它必須在我的calc類中,而不是main中。 我的程序可以執行我需要的工作,但calc可以運行總計。 對於我的一生,我無法弄清楚。 我將如何建立一種方法來保持總功能不在我的主要職能范圍內?

public class CalculatorImpl implements CalculatorInterface {

    private int total = 0;





    @Override
    public void reset() {
        // TODO Auto-generated method stub
        total = 0;

    }

    @Override
    public double plus(int a) {
        // TODO Auto-generated method stub
        total = total + a;

        return 0;
    }

    @Override
    public double minus(int a) {
        // TODO Auto-generated method stub
        total = total - a;

        return 0;
    }

    @Override
    public double star(int a) {
        // TODO Auto-generated method stub

        total = total * a;

        return 0;
    }

    @Override
    public double slash(int a) {
        // TODO Auto-generated method stub
        total = total / a;


        return 0;
    }

    @Override
    public double equal() {
        // TODO Auto-generated method stub

        return total;
    }


}

這是主程序

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        Random random = new Random(1000);

        CalculatorInterface calc = new CalculatorImpl();

        System.out.println("Answer 1: " + calc.equal()); // Print the initial
                                                        // value stored in
                                                        //calc should return 0

        // Test calc class 
        for (int i = 0; i < 26; i++) {
            // Get a random integer to select one of the four methods
            int r = random.nextInt(4);
            // Test statement for what it is selecting
            // System.out.println( "Int: " + r );
            switch (r) {
            case 0:
                calc.plus(random.nextInt(10));
                break;
            case 1:
                calc.minus(random.nextInt(10));
                break;
            case 2:
                calc.slash(random.nextInt(10));
                break;
            case 3:
                calc.star(random.nextInt(10));
                break;
            default:

            }

        }

        // Should return -218.0
        System.out.println("Answer 2: " + calc.equal());

        // Reset/clear
        calc.reset();

        // Should now have 0 in the calc, print 0
        System.out.println("Answer 3: " + calc.equal());

        // Simple example
        calc.plus(2);
        calc.minus(1);
        calc.star(5);
        calc.slash(10);

        // Should now have 0.5 in the calc, print 0.5
        System.out.println("Answer 4: " + calc.equal());

    }

}

這是另一堂課

public interface CalculatorInterface {

    public void reset();

    public double plus( int a );

    public double minus( int a );

    public double star( int a );

    public double slash( int a );

    public double equal();



}

總數是一個int ,因此不能保存值0.5 將其聲明為double

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM