一个 for 循环,用于将用户输入的数字放入文本字段中。它不会打印到字段或 System.out.println



我不确定代码是否有效,因为它没有打印到文本字段。我的代码:

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

有道理吗?

相关内容

  • 没有找到相关文章

最新更新