当前位置:   article > 正文

Java编写计算器_java计算器编程代码

java计算器编程代码

主要用JAVA编写一个计算器,能实现加减乘除、开平方、求阶乘等计算功能,计算器有图形化界面,能对异常进行处理,并且检验输入的合法性,对不合法的输入给出具体的提示信息
代码如下

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class CalculatorFrame extends JFrame implements ActionListener {
  5. private JTextField resultDisplay; //结果显示文本框
  6. private double num1; //第一个数字
  7. private double num2; //第二个数字
  8. private char operator = 'C'; //操作符,默认为清屏
  9. public CalculatorFrame()
  10. {
  11. super("计算器");
  12. JPanel mainPanel = new JPanel(); //主面板
  13. mainPanel.setLayout(new BorderLayout());
  14. resultDisplay = new JTextField(); //结果显示文本框
  15. resultDisplay.setEditable(false); //不能编辑
  16. mainPanel.add(resultDisplay, BorderLayout.NORTH); //添加到面板上方
  17. JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5)); //按钮面板,4行4列,行列间距为5
  18. addButton(buttonPanel, "7");
  19. addButton(buttonPanel, "8");
  20. addButton(buttonPanel, "9");
  21. addButton(buttonPanel, "÷"); //添加按钮:“7”、“8”、“9”、“÷”
  22. addButton(buttonPanel, "4");
  23. addButton(buttonPanel, "5");
  24. addButton(buttonPanel, "6");
  25. addButton(buttonPanel, "×"); //添加按钮:“4”、“5”、“6”、“×”
  26. addButton(buttonPanel, "1");
  27. addButton(buttonPanel, "2");
  28. addButton(buttonPanel, "3");
  29. addButton(buttonPanel, "-"); //添加按钮:“1”、“2”、“3”、“-”
  30. addButton(buttonPanel, "0");
  31. addButton(buttonPanel, ".");
  32. addButton(buttonPanel, "C");
  33. addButton(buttonPanel, "+"); //添加按钮:“0”、“.”、“C”、“+”
  34. addButton(buttonPanel, "x²");
  35. addButton(buttonPanel, "√");
  36. addButton(buttonPanel, "x!");
  37. addButton(buttonPanel, "="); //添加按钮:“x²”、“√”、“x!”、“=”
  38. mainPanel.add(buttonPanel, BorderLayout.CENTER); //添加按钮面板,位于主面板的中央
  39. JButton equalsButton = new JButton("="); //等于号按钮
  40. equalsButton.addActionListener(this); //添加事件响应器
  41. mainPanel.add(equalsButton, BorderLayout.SOUTH); //添加到主面板下方
  42. add(mainPanel); //将主面板添加到窗口中
  43. }
  44. private void addButton(Container panel, String label)
  45. {
  46. JButton button = new JButton(label); //新建按钮
  47. button.addActionListener(this); //添加事件响应器
  48. panel.add(button); //添加到按钮面板中
  49. }
  50. // 响应用户操作事件
  51. public void actionPerformed(ActionEvent evt)
  52. {
  53. String text = evt.getActionCommand(); //获取用户操作
  54. if (text.equals("C"))
  55. { //清屏
  56. resultDisplay.setText("");
  57. num1 = 0;
  58. num2 = 0;
  59. operator = 'C';
  60. return;
  61. }
  62. if (text.equals("x²"))
  63. { //平方
  64. num1 = Double.parseDouble(resultDisplay.getText()); //获取当前数字
  65. resultDisplay.setText(String.valueOf(num1 * num1)); //计算平方并显示
  66. operator = 'C';
  67. return;
  68. }
  69. if (text.equals("√"))
  70. { //开方
  71. num1 = Double.parseDouble(resultDisplay.getText()); //获取当前数字
  72. if (num1 < 0)
  73. { //负数不能开方
  74. resultDisplay.setText("不能对负数求平方根");
  75. return;
  76. }
  77. resultDisplay.setText(String.valueOf(Math.sqrt(num1))); //计算开方并显示
  78. operator = 'C';
  79. return;
  80. }
  81. if (text.equals("x!"))
  82. { //阶乘
  83. num1 = Double.parseDouble(resultDisplay.getText()); //获取当前数字
  84. resultDisplay.setText(String.valueOf(factorial(num1))); //计算阶乘并显示
  85. operator = 'C';
  86. return;
  87. }
  88. if (text.equals("+") || text.equals("-") || text.equals("×") ||
  89. text.equals("÷"))
  90. { //基本运算
  91. num1 = Double.parseDouble(resultDisplay.getText()); //获取第一个数字
  92. operator = text.charAt(0); //获取操作符
  93. resultDisplay.setText(""); //清空结果文本框
  94. return;
  95. }
  96. if (text.equals("="))
  97. { //计算结果
  98. num2 = Double.parseDouble(resultDisplay.getText()); //获取第二个数字
  99. switch (operator) { //选择合适的操作符进行计算
  100. case '+':
  101. resultDisplay.setText(String.valueOf(num1 + num2));
  102. break;
  103. case '-':
  104. resultDisplay.setText(String.valueOf(num1 - num2));
  105. break;
  106. case '×':
  107. resultDisplay.setText(String.valueOf(num1 * num2));
  108. break;
  109. case '÷':
  110. if (num2 == 0)
  111. { //除数不能为零
  112. resultDisplay.setText("除数不能为零");
  113. return;
  114. }
  115. resultDisplay.setText(String.valueOf(num1 / num2));
  116. break;
  117. }
  118. operator = 'C'; //清除操作符
  119. return;
  120. }
  121. resultDisplay.setText(resultDisplay.getText() + text); //输出数字或小数点
  122. }
  123. // 计算阶乘
  124. public static double factorial(double num)
  125. {
  126. if (num == 0)
  127. return 1;
  128. double result = 1;
  129. for (int i = 1; i <= num; i++)
  130. {
  131. result *= i;
  132. }
  133. return result;
  134. }
  135. public static void main(String[] args)
  136. {
  137. CalculatorFrame calculator = new CalculatorFrame();
  138. calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  139. calculator.setSize(300, 350);
  140. calculator.setVisible(true);
  141. }
  142. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/275864
推荐阅读
相关标签
  

闽ICP备14008679号