Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Sums */
- import javax.swing.*; //Importing for pretty user input windows
- public class Sums //Begin Sums class
- {
- public static void main(String args[]) //Begin main method
- {
- // variable Declarations
- int num;
- int total;
- String numString;
- //System welcome
- System.out.println("Welcome to the Sums program that will sum up all the numbers from 1 to your number. ");
- //Getting Input from users
- numString = JOptionPane.showInputDialog("Enter a number greater than or equal to zero");
- num= Integer.parseInt(numString);
- //Validate input (Selection Structure)
- while(num >= 0)
- total = (num * (1 + num)) / 2;
- System.out.println("The sums of the number entered is: " total);
- else
- System.out.println("Invalid number entered: " num);
- numString = JOptionPane.showInputDialog("Enter a number greater than or equal to zero");
- num= Integer.parseInt(numString);
- //Calculate the Sums (Loop)
- //Output the Sums total
- System.exit(0); //Ending program
- } //End main method
- } //End Sums
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement