java程序——两数的加减乘除

时间:2022-09-20 13:04:04

import javax.swing.JOptionPane; // import class JOptionPane

public class Elementary {

public static void main(String[] args) {
// TODO Auto-generated method stub

String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum, // sum of number1 and number2
poor, // poor of number1 and number2
product, // product of number1 and number2
consult; // consult of number1 and number2

// read in first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );

// read in second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );

// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );

// calculate the numbers
sum = number1 + number2;
poor = number1 - number2;
product = number1 * number2;
consult = number1 / number2;

// display the results
JOptionPane.showMessageDialog(
null, "The sum is " + sum + "\n" +
"The poor is " + poor + "\n" +
"The product is " + product + "\n" +
"The consult is " + poor, "Results",
JOptionPane.PLAIN_MESSAGE );

System.exit( 0 ); // terminate the program
}

}