2D数组仅显示Text函数中的最后一个元素

马文:

我的整个代码如下:

package calculator;

import java.util.Arrays;

import processing.core.PApplet;


public class Calculator extends PApplet  {

public static void main(String[] args) {
    main("calculator.Calculator");
}

float screenMargin = 40;
float padding = 15;

float w;
float h;

float totalMargin = 2 * screenMargin;

String[][] textToDisplay = new String[][] {
    {
        "1", "2", "3", "*"
    }, {
        "4", "5", "6", "+"
    }, {
        "7", "8", "9", "-"
    }, {
        "±", "0", ".", "="
    }
};
int rows = textToDisplay.length;
int cols = 4;

GridSquare[][] buttons;

public void settings() {
    size(400, 500);
}
public void setup() {

    background(255, 255, 255);
    for (String[] outerArray: textToDisplay) {
        for (String element: outerArray) {
            System.out.println(element);
        }
    }
    System.out.println(Arrays.deepToString(textToDisplay));
}
public void draw() {
    drawCalculator();
    drawButtons();
}

public void drawCalculator() {
    w = (width - totalMargin);
    h = (height - totalMargin);
    rectMode(CORNER);
    fill(125, 125, 125);
    rect(screenMargin, screenMargin, w, h);
    fill(255);
    rect(screenMargin + padding, screenMargin + padding, w - 2 * padding, h / 
8);
}

public void drawButtons()  {
    w = (width - totalMargin);
    h = (height - totalMargin);
    buttons = new GridSquare[rows][cols];
    float margin = 65;
    float calcMarginX;
    float dimensions = 45;
    float totalWidth = margin + dimensions * (cols - 1);
    float idontevencareanymore = 130;
    calcMarginX = w - totalWidth;
    calcMarginX /= 2;
    calcMarginX += screenMargin;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            buttons[i][j] = new GridSquare(i * margin + calcMarginX, j * 
margin + idontevencareanymore, dimensions, dimensions);
        }
    }
    for (int i = 0; i < textToDisplay.length; i++) {
        for (int j = 0; j < textToDisplay[i].length; j++) {
            buttons[i][j].displayText(textToDisplay);
        }
    }

}

public class GridSquare{

    public float x;
    public float y;
    public float ws;
    public float hs;

    public GridSquare(float tempX, float tempY, float tempW, float tempH)  {   
        x = tempX;
        y = tempY;
        ws = tempW;
        hs = tempH; 
    }

    public boolean onClick(float clickedX, float clickedY)  { 
        return (clickedX > x && clickedX < x + ws && clickedY > y && clickedY < y + hs);
    }

    public void draw() {
        fill(0, 30, 240);
        rectMode(CORNER);
        rect(x, y, ws, hs);

    }

    public void displayText(String[][] text2Display) {
        float squarePadding = 20;
        for (int i = 0; i < text2Display.length; i++) {
            for (int j = 0; j < text2Display[i].length; j++) {
                this.draw();
                textSize(20);
                fill(255, 255, 255);
                textAlign(CENTER);
                text(text2Display[i][j], x + squarePadding, y + 
squarePadding);
            //  System.out.println(text2Display[i][j]);
            }
        //  text(text2Display[i][0], x + squarePadding, y + squarePadding);
        }

    }
}


}

我的目标是使用Processing作为库,用Java制作计算器应用程序。我正在使用Eclipse,Processing和Proclipsing插件。

但是,当它遍历调用的二维数组textToDisplay时,对于一个文本命令,它仅显示数组中所有元素的最后一个元素。

为什么会这样呢?(我已经扫描了我的代码,从逻辑上讲似乎是正确的。)

我究竟做错了什么?(将其打印到控制台上显示了正确的代码,也为数组调用了文本函数。我感觉它处于for循环中。

我该如何解决?

我用其他语言看过类似的问题,但找不到它们有用。

Python类似示例

C类似的例子

PHP示例

马文:

调用该方法以及编写文本时的方法中,都有一个嵌套的for循环(两个for循环)从方法内部删除它,而是将其作为参数添加,然后将ij作为参数和参数传递给方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

访问结构中2D数组中一行的最后一个元素

2D数组中缺少最后一个元素

替换2D数组特定行中除最后一个元素以外的所有元素

为什么此代码仅显示数组中的最后一个元素?

仅显示最后一个元素

仅显示列表中的最后一个数组

显示2D数组错误严格标准的最后一个索引键

Python仅打印2d列表中的最后一个列表

JavaScript 饼图函数和回调仅显示数据库中的最后一个元素

制作一个接受数组输入的函数,打印2d c ++的元素

如何从numpy 2d数组中的每个元素创建一个邻居数组

循环仅显示最后一个数组元素ActionScript3

在C中声明2D数组仅知道一个的长度

numpy:索引3D数组,将最后一个轴的索引存储在2D数组中

仅显示链接列表的最后一个元素

Android:MySQL的ListView仅显示最后一个元素

jQuery .json中的JSON数组,当使用.append时,仅显示数组的最后一个值

嵌套v-for仅显示Vue中第一个元素的最后一个子元素

仅获取数组猫鼬的最后一个元素

仅保存C数组的最后一个元素

Java 2d数组向元素错误中添加一个

从python中的2d数组列表中选择一个随机元素

foreach函数仅显示最后一个值

创建动态数组仅显示最后一个

Node.js - 仅显示 JSON 响应中的最后一个元素

Listview在整个过程中仅显示最后一个元素

AngularJS仅显示JSON数据中的最后一个元素

ListView仅显示整个行中的最后一个元素

Firebase 数据库中仅显示最后一个元素