Advertisement
Guest User

code

a guest
Jun 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /* Sums  */
  2.  
  3. import javax.swing.*; //Importing for pretty user input windows
  4.  
  5. public class Sums //Begin Sums class
  6. {
  7.    public static void main(String args[]) //Begin main method
  8.    {
  9.    
  10.       // variable Declarations
  11.      
  12.       int num;
  13.       int total;
  14.      
  15.       String numString;
  16.            
  17.       //System welcome
  18.       System.out.println("Welcome to the Sums program that will sum up all the numbers from 1 to your number. ");
  19.  
  20.      
  21.       //Getting Input from users
  22.      
  23.       numString = JOptionPane.showInputDialog("Enter a number greater than or equal to zero");
  24.       num= Integer.parseInt(numString);
  25.      
  26.       //Validate input (Selection Structure)
  27.      
  28.       while(num >= 0)
  29.          total = (num * (1 + num)) / 2;
  30.          System.out.println("The sums of the number entered is: " total);
  31.      
  32.       else
  33.          System.out.println("Invalid number entered: " num);
  34.          numString = JOptionPane.showInputDialog("Enter a number greater than or equal to zero");      
  35.          num= Integer.parseInt(numString);
  36.      
  37.       //Calculate the Sums (Loop)
  38.      
  39.      
  40.       //Output the Sums total            
  41.            
  42.      
  43.       System.exit(0); //Ending program
  44.      
  45.    } //End main method
  46.  
  47. } //End Sums
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement