简体   繁体   English

使用单词而不是数字的Java数组

[英]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.

相关问题 为什么在Java中使用[]创建数组而不是创建数组对象? - Why are arrays created in Java using [] rather than creating an array object? 将单词直接插入db中的表中,而不是使用Java存储在arraylist中 - Insert words directly to table in db rather than storing in arraylist using Java 使用 Gson 反序列化 Java 中的 RouteXL 响应很困难,因为 RouteXL 返回多个对象而不是数组 - Deserialising RouteXL Response In Java using Gson Difficult as RouteXL Returns Multiple Objects Rather Than Array 使用扫描仪读取文本文件,为单词创建一个字符串,为数字生成一个int数组 - Using scanner to read text file and make a string for the words and an int array for the numbers Java 引用数组中的位置而不是数组中的数据。 (Java) - Referencing a Location in an Array rather than the Data within it. (Java) 在 Java 中按列而不是行平面映射二维数组 - Flatmapping a 2D array by column rather than row in Java 是否可以删除数组中的对象而不是其索引? (Java) - Is it possible to remove an object in an array rather than its index? (Java) 在 Java 中将单词转换为数字 - Converting words to numbers in Java 使用Java而不是JMS的WebSphere MQ类有什么好处? - What is the benefit of using WebSphere MQ classes for Java rather than JMS? 访问JSP Java scriptlet中的控制器方法而不是使用标签? - Access to controller methods in JSP Java scriptlet rather than using tags?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM