Sum Of Integers Do While

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Sum Of Integers Do While as PDF for free.

More details

  • Words: 217
  • Pages: 2
SumOfIntegersDoWhile /* * * Filename: "SumOfIntegersDoWhile.java" * Created by 1,000_naymes for ICt 352 * Purpose: to add integers up to and including the value entered by a user */ import javax.swing.JOptionPane; public class SumOfIntegersDoWhile { public static void main( String[] args ) { /************************************************************** declare and initialize variables **************************************************************/ String openingMessage, int1InputMessage, int1String, errorMessage, outputMessage; int int1 = 0, total = 0; openingMessage = "Welcome to 1KN's program, which will ask you for a positive integer and \ncompute the sum of all integers up to and including your number."; JOptionPane.showMessageDialog( null, openingMessage ); int1InputMessage = "Please enter your positive integer: "; int1String = JOptionPane.showInputDialog( int1InputMessage ); int1 = Integer.parseInt( int1String ); // converting string to double; do { for ( int i = int1; i > 0; i-- ) { total += i; } if(int1 < 0) { errorMessage = "Oops! You entered invalid data, please try again!"; JOptionPane.showMessageDialog( null, errorMessage ); int1InputMessage = "Please enter your positive integer: "; int1String = JOptionPane.showInputDialog( int1InputMessage ); int1 = Integer.parseInt( int1String ); // converting string to double; } else { outputMessage = "The sum of integers 1 through " +int1 + " is " + total; JOptionPane.showMessageDialog( null, outputMessage ); } } while( total < 1 ); System.exit(0); } //end main } //end class Page 1

SumOfIntegersDoWhile

Page 2

Related Documents

Sum Of Integers Do While
November 2019 15
Sum Of Integers
November 2019 10
Do-while
November 2019 32
Do While
June 2020 16