首页 > 其他 > 详细

编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果

时间:2016-10-25 02:31:18      阅读:230      评论:0      收藏:0      [点我收藏+]

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

}
}      

编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果

原文:http://www.cnblogs.com/6354-aa/p/5995197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!