首页 > 代码库 > 编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果
编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果
import java.util.Scanner;
import javax.swing.JOptionPane;
public class FourOperationsOfTwoIntegers {
public static void main(String[] args)
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
Double number1, // first number
number2;// second number
// result of operation between 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 = Double.parseDouble( firstNumber );
number2 = Double.parseDouble( secondNumber );
// the operation of the numbers
double result1=0.0,result2=0.0,result3=0.0,result4=0.0;
result1=number1+number2;
result2=number1-number2;
result3=number1*number2;
String str="俩数之和:"+number1+"+"+number2+"="+result1+"\n"
+"俩数之差:"+number1+"-"+number2+"="+result2+"\n"
+"俩数之积:"+number1+"x"+number2+"="+result3+"\n";
if(number2==0) {str+="The divisor "+number2+" is zero.Fail to divide!!!";}
else {result4=number1/number2;
str+="俩数之商:"+number1+"/"+number2+"="+result4+"\n";
}
JOptionPane.showMessageDialog(null, str,"任意两数的四则运算:",JOptionPane.INFORMATION_MESSAGE,null);
System.exit( 0 ); // terminate the program
}
}
编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果