1 package demo; 2 //An addition program 3 4 import javax.swing.JOptionPane; // import class JOptionPane 5 6 public class Addition { 7 public static void main( String args[] ) 8 { 9 String firstNumber, // first string entered by user 10 secondNumber; // second string entered by user 11 int number1, // first number to add 12 number2, // second number to add 13 sum, 14 cha,ji,shang;// sum of number1 and number2 15 16 // read in first number from user as a string 17 firstNumber = 18 JOptionPane.showInputDialog( "Enter first integer" ); 19 20 // read in second number from user as a string 21 secondNumber = 22 JOptionPane.showInputDialog( "Enter second integer" ); 23 24 // convert numbers from type String to type int 25 number1 = Integer.parseInt( firstNumber ); 26 number2 = Integer.parseInt( secondNumber ); 27 28 // add the numbers 29 sum = number1 + number2; 30 cha = number1 - number2; 31 ji = number1 * number2; 32 shang = (number1 / number2) + (number1 % number2); 33 34 // display the results 35 JOptionPane.showMessageDialog( 36 null, "The sum is " + sum 37 +"\n" + "The cha is:" + cha 38 +"\n" +"The ji is:" + ji 39 +"\n" +"The shang is:" + shang 40 ); 41 42 System.exit( 0 ); // terminate the program 43 } 44 }
时间: 2024-10-09 17:07:28