二维数组仅显示文本函数中的最后一个元素
2D Array only showing last element in Text function
我的整个代码如下:
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 循环中。
我该如何解决这个问题?
我查看了其他语言的类似问题,但没有发现它们有用。
C Similar Example
以后,请尝试 post MCVE 而不是 post 完整的程序。
我能给你的最好建议是debug your code。跟踪代码以准确了解哪一行执行的操作与您的预期不同。
你有几行是这样的:
rect(x, y, ws, hs);
这行执行时x
、y
、ws
、hs
的值是多少?找到其他类似的行并打印出这些值。这些值是否与您的预期不同?
如果您仍然遇到困难,那么我建议 breaking your problem down into smaller steps 并一次一个地执行这些步骤。你能用一个按钮创建一个简单的计算器吗?现在可以添加第二个按钮了吗?在尝试添加更多按钮之前先让它工作。
然后,如果您卡在其中一个较小的步骤上,您可以 post 一个更具体的技术问题以及 MCVE。祝你好运。
你有一个嵌套的 for 循环(2 个 for 循环)围绕调用方法作为 well 就像在编写文本时的方法一样。将其从方法内部移除,而是将其添加为参数,然后将 i
和 j
作为参数和参数传递给方法。
我的整个代码如下:
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 循环中。
我该如何解决这个问题?
我查看了其他语言的类似问题,但没有发现它们有用。
C Similar Example
以后,请尝试 post MCVE 而不是 post 完整的程序。
我能给你的最好建议是debug your code。跟踪代码以准确了解哪一行执行的操作与您的预期不同。
你有几行是这样的:
rect(x, y, ws, hs);
这行执行时x
、y
、ws
、hs
的值是多少?找到其他类似的行并打印出这些值。这些值是否与您的预期不同?
如果您仍然遇到困难,那么我建议 breaking your problem down into smaller steps 并一次一个地执行这些步骤。你能用一个按钮创建一个简单的计算器吗?现在可以添加第二个按钮了吗?在尝试添加更多按钮之前先让它工作。
然后,如果您卡在其中一个较小的步骤上,您可以 post 一个更具体的技术问题以及 MCVE。祝你好运。
你有一个嵌套的 for 循环(2 个 for 循环)围绕调用方法作为 well 就像在编写文本时的方法一样。将其从方法内部移除,而是将其添加为参数,然后将 i
和 j
作为参数和参数传递给方法。