欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单计算器功能

程序员文章站 2022-07-05 20:09:08
使用java来实现一个简单的计算器,供大家参考,具体内容如下最近使用java制作了一个计算器,菜单栏里的功能没有实现,只是写一个版式,仅仅实现了计算功能 ,如果想要实现菜单栏功能可自行在该代码的基础上...

使用java来实现一个简单的计算器,供大家参考,具体内容如下

最近使用java制作了一个计算器,菜单栏里的功能没有实现,只是写一个版式,仅仅实现了计算功能 ,如果想要实现菜单栏功能可自行在该代码的基础上去实现。

话不多说直接上代码

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;

 public class calculate extends jframe implements actionlistener {
  private container ct;
  private jbutton[] jbs;
  private string[] str;
  private jtextfield jtextfield;
  //cal[0]用来存储第一次输入的数
  //cal[1]用来存储运算符(+,-,*,/,%)
  //cal[2]用来存储第二次输入的数
  //cal[3]用来存储结果
  //cal[4]用来存储ms键所存储的数
  //cal[5]用来获取要存入的值(用来给cal[4]赋值)
  private string[] cal = {"","","","","",""};
  //_count和_count1始终相反,如果_count为true则表示正在输入cal[0]反之表示cal[0]输入完成,该输入cal[2]
  //当点过等号后将cal[3]的值赋给cal[0]
  private boolean _count = true;
  private boolean _count1 = false;

  public calculate() {
   super("计算器");
   jbs = new jbutton[28];
   ct = this.getcontentpane();
   this.setbounds(100, 100, 315, 400);
   this.setdefaultcloseoperation(windowconstants.exit_on_close);
   ct.setlayout(null);
   //菜单栏
   jmenubar jmenubar = new jmenubar();
   jmenu jm = new jmenu("查看(v)");
   jmenu jm1 = new jmenu("编辑(e)");
   jmenu jm2 = new jmenu("帮助(h)");
   jmenubar.add(jm);
   jmenubar.add(jm1);
   jmenubar.add(jm2);
   this.setjmenubar(jmenubar);
   //输出框
   jtextfield = new jtextfield("0");
   jtextfield.setfont(new font("宋体", font.bold, 25));
   jtextfield.seteditable(false);
   jtextfield.sethorizontalalignment(jtextfield.right);
   jtextfield.setbounds(5, 5, 285, 65);
   ct.add(jtextfield);
   //按键    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   str = new string[]{"mc", "mr", "ms", "m+", "m-", "←", "ce", "c", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"};
   int k = 0;
   for (int i = 75; i <= 195; i = i + 40) {
    for (int init = 10; init <= 290; init = init + 57) {
     jbutton jbutton = new jbutton(str[k]);
     jbs[k] = jbutton;
     jbutton.setbounds(init, i, 52, 35);
     ct.add(jbutton);
     k++;
     system.out.println(k);
    }
   }
   for (int i = 235; i < 265; i = i + 40) {
    for (int j = 10; j <= 181; j = j + 57) {
     jbutton jbutton = new jbutton(str[k]);
     jbutton.setbounds(j, i, 52, 35);
     ct.add(jbutton);
     jbs[k] = jbutton;
     k++;
     system.out.println(k);
    }
   }
   jbutton jbutton = new jbutton(str[k]);
   jbs[k] = jbutton;
   k++;
   system.out.println(k);
   jbutton.setbounds(240, 235, 52, 75);
   ct.add(jbutton);
   jbutton jbutton2 = new jbutton(str[k]);
   system.out.println(str[k]);
   jbutton2.setbounds(10, 275, 109, 35);
   ct.add(jbutton2);
   jbs[k] = jbutton2;
   k++;
   for (int i = 275; i < 310; i = i + 40) {
    for (int j = 124; j <= 181; j = j + 57) {
     jbutton jbutton1 = new jbutton(str[k]);
     jbutton1.setbounds(j, i, 52, 35);
     ct.add(jbutton1);
     jbs[k] = jbutton1;
     k++;
    }
   }
   for (int i = 0; i < 28; i++) {
    jbs[i].addactionlistener(this);
   }
   this.setvisible(true);
  }

  public static void main(string[] args) {
   new calculate();
  }


  @override
  public void actionperformed(actionevent actionevent) {
   if (actionevent.getactioncommand().equals(".")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("0")){
    method(actionevent);
   } else if (actionevent.getactioncommand().equals("1")){
    method(actionevent);
   }else if(actionevent.getactioncommand().equals("2")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("3")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("4")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("5")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("6")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("7")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("8")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("9")){
    method(actionevent);
   }else if (actionevent.getactioncommand().equals("±")){
    if(_count){
     if (cal[0].contains(".")){
      cal[0] = string.valueof(double.parsedouble(cal[0]) * (-1));
      jtextfield.settext(cal[0]);
     }else {
      cal[0] = string.valueof(integer.parseint(cal[0]) * (-1));
      jtextfield.settext(cal[0]);
     }
    }else {
     if (cal[2].contains(".")){
      cal[2] = string.valueof(double.parsedouble(cal[2]) * (-1));
      jtextfield.settext(cal[2]);
     }else {
      cal[2] = string.valueof(integer.parseint(cal[2]) * (-1));
      jtextfield.settext(cal[2]);
     }
    }
   }else if(actionevent.getactioncommand().equals("√")){
    if(_count){
     cal[0] = string.valueof(math.pow(double.parsedouble(cal[0]),0.5));
     jtextfield.settext(cal[0]);
    }else {
     cal[2] = string.valueof(math.pow(double.parsedouble(cal[2]),0.5));
     jtextfield.settext(cal[2]);
    }
   }else if(actionevent.getactioncommand().equals("1/x")){
    if(_count){
     cal[0] = string.valueof(1/double.parsedouble(cal[0]));
     jtextfield.settext(cal[0]);
    }else {
     cal[2] = string.valueof(1/double.parsedouble(cal[2]));
     jtextfield.settext(cal[2]);
    }
   }else if(actionevent.getactioncommand().equals("←")){
    if(_count){
     cal[0] = cal[0].substring(0,cal[0].length()-1);
     if(cal[0].equals("")){
      jtextfield.settext("0");
     }else {
      jtextfield.settext(cal[0]);
     }
    }else {
     cal[2] = cal[2].substring(0,cal[2].length()-1);
     if(cal[2].equals("")){
      jtextfield.settext("0");
     }else {
      jtextfield.settext(cal[2]);
     }
    }
   }else if(actionevent.getactioncommand().equals("ce")){
    if (_count){
     cal[0]="";
     jtextfield.settext("0");
    }else {
     cal[2]="";
     jtextfield.settext("0");
    }
   }else if(actionevent.getactioncommand().equals("c")){
    _count = true;
    cal[0]="";
    cal[1]="";
    cal[2]="";
    cal[3]="";
    jtextfield.settext("0");
   }else if (actionevent.getactioncommand().equals("+")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=string.valueof(double.parsedouble(cal[0]) + double.parsedouble(cal[2]));
     }else {
      cal[0] = string.valueof(integer.parseint(cal[0]) + integer.parseint(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "+";
    _count=false;
    jtextfield.settext(cal[1]);
   }else if (actionevent.getactioncommand().equals("-")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=string.valueof(double.parsedouble(cal[0]) - double.parsedouble(cal[2]));
     }else {
      cal[0] = string.valueof(integer.parseint(cal[0]) - integer.parseint(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "-";
    _count=false;
    jtextfield.settext(cal[1]);
   }else if (actionevent.getactioncommand().equals("*")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=string.valueof(double.parsedouble(cal[0]) * double.parsedouble(cal[2]));
     }else {
      cal[0] = string.valueof(integer.parseint(cal[0]) * integer.parseint(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "*";
    _count=false;
    jtextfield.settext(cal[1]);
   }else if (actionevent.getactioncommand().equals("/")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=string.valueof(double.parsedouble(cal[0]) / double.parsedouble(cal[2]));
     }else {
      cal[0] = string.valueof(integer.parseint(cal[0]) / integer.parseint(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "/";
    _count=false;
    jtextfield.settext(cal[1]);
   }else if (actionevent.getactioncommand().equals("%")){
    cal[0]=string.valueof(integer.parseint(cal[0]) % integer.parseint(cal[2]));
    cal[2]="";
    cal[1] = "%";
    _count=false;
    jtextfield.settext(cal[1]);
   }else if (actionevent.getactioncommand().equals("=")){
    if(cal[0].equals("")&&cal[2].equals("")){
     jtextfield.settext("0");
    }else if(!cal[0].equals("")&&cal[2].equals("")){
     jtextfield.settext(cal[0]);
    }else if(!cal[0].equals("")&&!cal[2].equals("")){
     if(cal[1].equals("+")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=string.valueof(double.parsedouble(cal[0]) + double.parsedouble(cal[2]));
      }else {
       cal[3] = string.valueof(integer.parseint(cal[0]) + integer.parseint(cal[2]));
      }
      jtextfield.settext(cal[3]);
     }else if (cal[1].equals("-")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=string.valueof(double.parsedouble(cal[0]) - double.parsedouble(cal[2]));
      }else {
       cal[3] = string.valueof(integer.parseint(cal[0]) - integer.parseint(cal[2]));
      }
      jtextfield.settext(cal[3]);
     }else if(cal[1].equals("*")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=string.valueof(double.parsedouble(cal[0]) * double.parsedouble(cal[2]));
      }else {
       cal[3] = string.valueof(integer.parseint(cal[0]) * integer.parseint(cal[2]));
      }
      jtextfield.settext(cal[3]);
     }else if (cal[1].equals("/")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=string.valueof(double.parsedouble(cal[0]) / double.parsedouble(cal[2]));
      }else {
       cal[3] = string.valueof(integer.parseint(cal[0]) / integer.parseint(cal[2]));
      }
      jtextfield.settext(cal[3]);
     }else if (cal[1].equals("%")){
      cal[3]=string.valueof(integer.parseint(cal[0]) % integer.parseint(cal[2]));
      jtextfield.settext(cal[3]);
     }
    }
    cal[0]=cal[3];
    cal[2]="";
    _count=true;
    _count1 = true;
    cal[5]=cal[0];
   }else if(actionevent.getactioncommand().equals("mc")){
    cal[4]="";
   }else if(actionevent.getactioncommand().equals("mr")){
    if(_count){
     if(!cal[4].equals("")){
      cal[0]=cal[4];
      jtextfield.settext(cal[0]);
     }
    }else {
     if(!cal[4].equals("")){
      cal[2]=cal[4];
      jtextfield.settext(cal[2]);
     }
    }
   }else if(actionevent.getactioncommand().equals("ms")){
    if(_count){
     cal[0]="";
    }
    cal[4]=cal[5];
    system.out.println("cal[5]"+cal[5]);
    system.out.println("ms:"+cal[4]);
   }else if(actionevent.getactioncommand().equals("m+")){
    if(!cal[4].equals("")){
     if (cal[4].contains(".") || cal[5].contains(".")){
      cal[4]=string.valueof(double.parsedouble(cal[4]) + double.parsedouble(cal[5]));
     }else {
      cal[4] = string.valueof(integer.parseint(cal[4]) + integer.parseint(cal[5]));
     }
    }
   }else if(actionevent.getactioncommand().equals("m-")){
    if(!cal[4].equals("")){
     if (cal[4].contains(".") || cal[5].contains(".")){
      cal[4]=string.valueof(double.parsedouble(cal[4]) - double.parsedouble(cal[5]));
     }else {
      cal[4] = string.valueof(integer.parseint(cal[4]) - integer.parseint(cal[5]));
     }
    }
   }
  }
  public void method(actionevent actionevent){
   if(_count){
    if (_count1){
     cal[0]="";
     _count1=false;
    }
    cal[0] += actionevent.getactioncommand();
    cal[5]=cal[0];
    system.out.println(cal[5]);
   }else {
    cal[2] += actionevent.getactioncommand();
    cal[5]=cal[2];
   }
   jtextfield.settext(actionevent.getactioncommand());
  }
}

效果图:

java实现简单计算器功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: java 计算器