IntegerMath /* * Filename: "IntegerMath.java" * Created by 1,000_naymes for ICT 352 * * Purpose: using a do... while loop & switch options to to add, multiply or quit * based on input from the user. * */ import javax.swing.JOptionPane; public class IntegerMath { public static void main( String[] args ) { /************************************************************** declare and initialize variables **************************************************************/ String openingMessage, number1InputMessage, number1String, number2InputMessage, number2String, number3InputMessage, number3String, number4InputMessage, number4String, number5InputMessage, number5String, menu, addMessage, productMessage, quitMessage = "Goodbye.", choiceInputMessage, choiceString; int number1, number2, number3, number4, number5, sum, product, choice; /************************************************************** display opening message **************************************************************/ openingMessage = "Welcome to 1KN's IntegerMath program, \nwhich will add or multiply 5 numbers for you."; JOptionPane.showMessageDialog( null, openingMessage ); do { /************************************************************** user choice **************************************************************/ menu = "Enter \n" + "1 to add your integers\n" + "2 to multiply your integers\n" + "3 to quit"; choiceString = JOptionPane.showInputDialog( menu ); choice = Integer.parseInt( choiceString ); //converting string to integer switch( choice ) { case 1: /************************************************************** input for first number **************************************************************/ number1InputMessage = "Please enter your first whole number."; number1String = JOptionPane.showInputDialog( number1InputMessage ); number1 = Integer.parseInt( number1String ); //converting string to integer Page 1
IntegerMath /************************************************************** input for second number **************************************************************/ number2InputMessage = "Please enter your second whole number."; number2String = JOptionPane.showInputDialog( number2InputMessage ); number2 = Integer.parseInt( number2String ); //converting string to integer /************************************************************** input for third number **************************************************************/ number3InputMessage = "Please enter your third whole number."; number3String = JOptionPane.showInputDialog( number3InputMessage ); number3 = Integer.parseInt( number3String ); //converting string to integer /************************************************************** input for fourth number **************************************************************/ number4InputMessage = "Please enter your fourth whole number."; number4String = JOptionPane.showInputDialog( number4InputMessage ); number4 = Integer.parseInt( number4String ); //converting string to integer /************************************************************** input for fifth number **************************************************************/ number5InputMessage = "Please enter your fifth whole number."; number5String = JOptionPane.showInputDialog( number5InputMessage ); number5 = Integer.parseInt( number5String ); //converting string to integer /************************************************************** running calculations **************************************************************/ sum = ( number1 + number2 + number3 + number4 + number5 ); addMessage = "You entered " + number1 + ", " + number2 + ", " + number3 + ", " + number4 + ", and " + number5 + ". \nThe sum of your numbers is " + sum; JOptionPane.showMessageDialog( null, addMessage ); break; case 2: /************************************************************** input for first number **************************************************************/ number1InputMessage = "Please enter your first whole number."; number1String = JOptionPane.showInputDialog( number1InputMessage ); number1 = Integer.parseInt( number1String ); //converting string to integer /************************************************************** input for second number **************************************************************/ number2InputMessage = "Please enter your second whole number."; number2String = JOptionPane.showInputDialog( number2InputMessage ); number2 = Integer.parseInt( number2String ); //converting string to integer /************************************************************** input for third number **************************************************************/ number3InputMessage = "Please enter your third whole number."; Page 2
IntegerMath number3String = JOptionPane.showInputDialog( number3InputMessage ); number3 = Integer.parseInt( number3String ); //converting string to integer /************************************************************** input for fourth number **************************************************************/ number4InputMessage = "Please enter your fourth whole number."; number4String = JOptionPane.showInputDialog( number4InputMessage ); number4 = Integer.parseInt( number4String ); //converting string to integer /************************************************************** input for fifth number **************************************************************/ number5InputMessage = "Please enter your fifth whole number."; number5String = JOptionPane.showInputDialog( number5InputMessage ); number5 = Integer.parseInt( number5String ); //converting string to integer /************************************************************** running calculations **************************************************************/ product = ( number1 * number2 * number3 * number4 * number5 ); productMessage = "You entered " + number1 + ", " + number2 + ", " + number3 + ", " + number4 + ", and " + number5 + ". \n The product of your numbers is " + product; JOptionPane.showMessageDialog( null, productMessage ); break; case 3: JOptionPane.showMessageDialog( null, quitMessage ); break; } } while(choice != 3 ); System.exit(0); } }
Page 3