[英]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. 我想做的是使用这个Bar类创建一个图形,它实际上只是一个可编辑的矩形。此循环的每个周期我应该输入2个数字,我在另一个输入第一个数字的类中调用数组将该数组放入当前的Bar y中,并在另一段代码中使用该数字并对其执行运算并返还Bar x的税款,我认为我的问题源于我渲染相同的事实一遍又一遍,所以我想知道是否有一种方法可以在for循环中创建具有不同名称的多个对象,从而允许它前进到数组的下一个值。
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. 循环使用的是在循环之前初始化的数据,因此
income
将始终为values[0]
,并且所有条形仅表示第一个值。
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 移动大部分代码(
TaxCalculator
部分可以留在外面,因为它很可能是可重用的)将为您提供一些类似于
for(int i = 0;i < values.length; i++) {
double income = values[i];
double tax = one.tax(income);
Bar y = new Bar();
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.