我不确定代码是否有效,因为它没有打印到文本字段。我的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int startNum = 0, endNum = 0, difference;
difference = endNum - startNum;
startNum = Integer.parseInt(startInput.getText());
endNum = Integer.parseInt(endInput.getText());
if(difference > 1)
{
for (int i = startNum; i <= endNum; i++)
{
output.setText(Integer.toString(i));
}
}
else if(difference < 1)
{
for (int i = startNum; i >= endNum; i--)
{
output.setText(Integer.toString(i));
}
}
}
代码:
int startNum = 0, endNum = 0, difference;
difference = endNum - startNum;//<--WRONG, it should be after the next two lines. Because you are setting startNum and endNum values to zero and you are calculating the difference.
startNum = Integer.parseInt(startInput.getText());
endNum = Integer.parseInt(endInput.getText());// But after taking the input you are not calculating the difference
正确的代码:
int startNum = 0, endNum = 0, difference;
startNum = Integer.parseInt(startInput.getText());
endNum = Integer.parseInt(endInput.getText());
difference = endNum - startNum;//<--HERE, this is where it should be
有道理吗?