简单的java程序通过对话框输出 计算加减乘除运算(运算方法可选择)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的java程序通过对话框输出 计算加减乘除运算(运算方法可选择)相关的知识,希望对你有一定的参考价值。

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

public class Addition {
   public static void main( String args[] )
   {
      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
          mul ,sub,div; 
      // 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 );

      // add the numbers
      sum = number1 + number2;
      mul = number1*number2;
      sub = number1-number2;
      div = number1/number2;
      String a;
       a=JOptionPane.showInputDialog("please choose formula mode  sum  sub  mul  div");
       
      // display the results
       if(a.equals("+"))
      JOptionPane.showMessageDialog(
         null, "The sum is " + sum, "Results",
         JOptionPane.PLAIN_MESSAGE );
       if(a.equals("-"))
    	   JOptionPane.showMessageDialog(
    		         null, "The sum is " + sub, "Results",
    		         JOptionPane.PLAIN_MESSAGE );
       if(a.equals("*"))
    	   JOptionPane.showMessageDialog(
    		         null, "The sum is " + mul, "Results",
    		         JOptionPane.PLAIN_MESSAGE );
       if(a.equals("/"))
    	   JOptionPane.showMessageDialog(
    		         null, "The sum is " + div, "Results",
    		         JOptionPane.PLAIN_MESSAGE );
        System.exit( 0 );   // terminate the program
   }
}

  

以上是关于简单的java程序通过对话框输出 计算加减乘除运算(运算方法可选择)的主要内容,如果未能解决你的问题,请参考以下文章

怎样用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器?

课堂Java小程序(加减乘除与验证码)

简单计算器

c++简单复数计算器

java课程设计:设计一个计算器模拟程序。

求简单java写计算器代码加减乘除