简体   繁体   中英

Java array using words rather than numbers

This is the chunk of code that I am having issues with:

  public void draw() {
        double tax = 0;
        double income = values[0];
        TaxCalculator one = new TaxCalculator();
        tax = one.tax(income);
        double net = 0;
        net = one.net(income);


        for(int i = 0; i<values.length;i++){
            Bar y = new Bar();
            y.makeVisible();

            y.moveHorizontal(20 + i*20);
            y.moveVertical(197 - (int) income);
            y.changeSize(3, (int) income);
            y.changeColour(Colour.BLUE);

            Bar x = new Bar();
            x.makeVisible();

            x.moveHorizontal(20 + i*20);
            x.moveVertical(197 - (int) income - (int) tax);
            x.changeSize(3, (int) tax);
            x.changeColour(Colour.RED);

            } 

what I am trying to do is create a graph using this Bar class, which is essentially just a editable rectangle.I have 2 numbers per cycle of this loop that should be input, I am calling a array in another class which inputs the first number of that array into what is currently Bar y, and in another chunk of code it is taking that number and performing an operation on it and returning tax which is Bar x, I believe my issue is stemming from the fact that I am rendering the same bar over and over, so I am wondering if there is a way to create multiple objects with different names within a for loop allowing it to progress onto the next value of my array.

If more of my code is needed to resolve this I am happy to post it

The loop is using the data that is initialized before the loop, so income will always be values[0] , and all the bars will only represent the first value.

Moving most of the code (the TaxCalculator part can stay outside, since it's most likely reusable) will give you something along the lines of

for(int i = 0;i < values.length; i++) {
    double income = values[i];
    double tax = one.tax(income);

    Bar y = new Bar();
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM