public void calculate(int input) {
inputField.setText("" + input);
while (input >= 1) {
if (input % 2 == 0) {
input = input / 2;
} else {
input = (input * 3) + 1;
}
output.append("" + input);
}
}
The output variable is a JTextArea and inputField is a JTextField where the user enters an integer.
I call the method and initialize the input variable here:
@Override
public void actionPerformed(ActionEvent e) {
input = Integer.parseInt(inputField.getText());
calculate(input);
}
}
Every time the value of input is even, it is divided by 2 so it should eventually reach 1, correct? Then why does this while loop cause an infinite loop?