import javax.swing.*; import java.text.DecimalFormat; /** * * @author rgilbert */ public class Main { /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { // Declare variables int n; float sum = 0.0f; float mysqrt = 0.0f; String sOutput = ""; // Decimal Format. DecimalFormat formatter = new DecimalFormat("#0.0000"); // Loop for( n=1; n <= 999 ;n++ ) { mysqrt = (float) Math.sqrt((float) n); sum += mysqrt; sOutput += formatter.format(mysqrt); if ( n % 3 == 0 ) { sOutput += "\t" + formatter.format(sum) + "\n"; sum = 0.0f; } else { sOutput += "\t"; } } //Create and set up the window. JFrame frame = new JFrame("Loop Exercise"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea area = new JTextArea(sOutput, 30, 50); JScrollPane scroll = new JScrollPane(area); frame.getContentPane().add(scroll); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }