如何在文本框中显示循环语句?
How do you display a looping statement in a text box?
try {
num = Integer.parseInt(userText.getText());
for (i = 1; i <= num; i++) {
if (num % i == 0)
textFieldAns.setText(" "+i);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Please Enter Valid number");
}
输出似乎没有以循环语句的形式显示数字的因数。
您应该将全文构建在 StringBuilder
:
int num = Integer.parseInt(userText.getText());
StringBuilder buf = new StringBuilder();
for (i = 1; i <= num; i++) {
if (num % i == 0)
buf.append(" ").append(i);
}
textFieldAns.setText(buf.substring(1));
try {
num = Integer.parseInt(userText.getText());
for (i = 1; i <= num; i++) {
if (num % i == 0)
textFieldAns.setText(" "+i);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Please Enter Valid number");
}
输出似乎没有以循环语句的形式显示数字的因数。
您应该将全文构建在 StringBuilder
:
int num = Integer.parseInt(userText.getText());
StringBuilder buf = new StringBuilder();
for (i = 1; i <= num; i++) {
if (num % i == 0)
buf.append(" ").append(i);
}
textFieldAns.setText(buf.substring(1));