gpt4 book ai didi

即使工作正常,Java 方法创建也无法读取

转载 作者:行者123 更新时间:2023-12-01 12:23:50 26 4
gpt4 key购买 nike

我想创建一个方法,可以简单地从 Swing 表单中的文本字段中获取文本。然而,试图让它被我的“if 语句”之一识别最终会出现一个错误,告诉我尽管已定义该符号,但它无法找到该符号。我还想创建更清晰的代码,因为我这样做的方式似乎相当重复和困惑;有没有办法缩短重复次数并将它们也称为变量?

    public class TextGrabber {
int [] grabText = {Integer.parseInt(DigitValue.getText())};
}

int digit1 = TextGrabber;
if (digit1 == 1) {
Digi1.setVisible(false);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(false); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(false);
}

int digit2 = Integer.parseInt(DigitValue.getText());
if (digit2 == 2) {
Digi1.setVisible(true);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(false);
Digi7.setVisible(true);
}

int digit3 = Integer.parseInt(DigitValue.getText());
if (digit3 == 3) {
Digi1.setVisible(true);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(true);
}

int digit4 = Integer.parseInt(DigitValue.getText());
if (digit4 == 4) {
Digi1.setVisible(false);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(false);
}

int digit5 = Integer.parseInt(DigitValue.getText());
if (digit5 == 5) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(false);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(true);
}

int digit6 = Integer.parseInt(DigitValue.getText());
if (digit6 == 6) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(false);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(true);
Digi7.setVisible(true);
}

int digit7 = Integer.parseInt(DigitValue.getText());
if (digit7 == 7) {
Digi1.setVisible(true);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(false);
}

int digit8 = Integer.parseInt(DigitValue.getText());
if (digit8 == 8) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(true);
Digi7.setVisible(true);
}

int digit9 = Integer.parseInt(DigitValue.getText());
if (digit9 == 9) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

int AddOneDigit = Integer.parseInt(DigitValue.getText());
AddOneDigit = AddOneDigit + 1;
String NewSet = Integer.toString (AddOneDigit);
DigitValue.setText(NewSet);
ShowDigitActionPerformed(evt);

int check = Integer.parseInt(DigitValue.getText());
if (check >= 10) {
check = 0;
String CheckS = Integer.toString (check);
DigitValue.setText(CheckS);
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(false); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(true);
Digi7.setVisible(true);
}

最佳答案

将蒙版存储为数组,然后使用类似的内容构建它

int digit = Integer.parseInt(DigitValue.getText());
boolean[][] mask = { { false, false, true, false, false, true, false },
{ true, false, true, true, true, false, true },
{ true, false, true, true, false, true, true },
{ false, true, true, true, false, true, false },
{ true, true, false, true, false, true, true },
{ true, true, false, true, true, true, true },
{ true, false, true, true, false, true, false },
{ true, true, true, true, true, true, true },
{ true, true, true, true, false, true, true } };
boolean[] digits = mask[digit - 1];
Digi1.setVisible(digits[0]);
Digi2.setVisible(digits[1]);
Digi3.setVisible(digits[2]);
Digi4.setVisible(digits[3]);
Digi5.setVisible(digits[4]);
Digi6.setVisible(digits[5]);
Digi7.setVisible(digits[6]);

我还注意到您没有数字'0'的映射。

关于即使工作正常,Java 方法创建也无法读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26495327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com