I am learning Java and have a fairly simple program that returns a series of numbers according to the Collatz hypothesis . I can output it to the console or pop up a lot of JOptionPane.showMessageDialog() windows, one with each number in it.
How would I combine JOptionPane.showMessageDialog() to show all the outputs in one JOptionPane.showMessageDialog() ?
The code:
package collatz; import java.util.Random; import javax.swing.*; public class Collatz { public static void main(String[] args) { Random randomGenerator = new Random(); int n = randomGenerator.nextInt(1000); JOptionPane.showMessageDialog(null, "The randomly generated number was: " + n); while(n > 1){ if(n % 2 == 0){ n = n / 2; JOptionPane.showMessageDialog(null, n); } else{ n = 3 * n + 1; JOptionPane.showMessageDialog(null, n); } } JOptionPane.showMessageDialog(null, n); JOptionPane.showMessageDialog(null, "Done."); } }
Thanks!
- ZuluDeltaNiner
source share