编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果
Posted 耶路撒冷有多冷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果相关的知识,希望对你有一定的参考价值。
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
}
}
以上是关于编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果的主要内容,如果未能解决你的问题,请参考以下文章
用Java编一个程序接受用户输入两个整数num1和num2,求出这两个数之间所有奇数的和.
编写程序,从键盘输入一个三位数,求出其逆序数并输出,例如输入127,输出721。