[Java] 纯文本查看 复制代码package cc;

import java.lang.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class cc {

static ButtonFrame f = new ButtonFrame();

public static void main(String[] args) {

//JOptionPane.showMessageDialog(null, "Code By Sreac.L","About", 1);

f.setTitle("计算器");

f.setBounds(300, 200, 250, 300);

f.setVisible(true);

}

}

class ButtonFrame extends JFrame implements ActionListener {

static TextField t, t1;

static Label l;

float temp1 = 0, r = 0;

String temp2 = "", ysf = "";

public ButtonFrame() {

setLayout(null);

// JTextArea t = new JTextArea();

l = new Label();

l.setLocation(5, 222);

l.setSize(230, 50);

add(l);

t = new TextField("计算、结果_文本框,支持连续四则运算", 50);

t.setBounds(5, 2, 224, 35);

add(t);

t1 = new TextField("运算式预览_文本框,不可编辑", 50);

t1.setBounds(5, 40, 224, 30);

add(t1);

t1.setEditable(false);

Button yu = new Button("%");

yu.setBounds(165, 155, 50, 30);

add(yu);

Button gen = new Button("√");

gen.setBounds(165, 190, 50, 30);

add(gen);

Button chu = new Button("÷");

chu.setBounds(125, 85, 30, 30);

add(chu);

Button cheng = new Button("×");

cheng.setBounds(125, 120, 30, 30);

add(cheng);

Button jia = new Button("+");

jia.setBounds(125, 155, 30, 30);

add(jia);

Button jian = new Button("-");

jian.setBounds(125, 190, 30, 30);

add(jian);

Button q = new Button("清除");

q.setBounds(165, 85, 50, 30);

add(q);

Button b = new Button("Back");

b.setBounds(165, 120, 50, 30);

add(b);

Button b7 = new Button("7");

b7.setBounds(5, 85, 30, 30);

add(b7);

Button b8 = new Button("8");

b8.setBounds(45, 85, 30, 30);

add(b8);

Button b9 = new Button("9");

b9.setBounds(85, 85, 30, 30);

add(b9);

Button b4 = new Button("4");

b4.setBounds(5, 120, 30, 30);

add(b4);

Button b5 = new Button("5");

b5.setBounds(45, 120, 30, 30);

add(b5);

Button b6 = new Button("6");

b6.setBounds(85, 120, 30, 30);

add(b6);

Button b1 = new Button("1");

b1.setBounds(5, 155, 30, 30);

add(b1);

Button b2 = new Button("2");

b2.setBounds(45, 155, 30, 30);

add(b2);

Button b3 = new Button("3");

b3.setBounds(85, 155, 30, 30);

add(b3);

Button bd = new Button(".");

bd.setBounds(5, 190, 30, 30);

add(bd);

Button b0 = new Button("0");

b0.setBounds(45, 190, 30, 30);

add(b0);

Button dy = new Button("=");

dy.setBounds(85, 190, 30, 30);

add(dy);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

dy.addActionListener(this);

bd.addActionListener(this);

q.addActionListener(this);

b.addActionListener(this);

cheng.addActionListener(this);

chu.addActionListener(this);

jian.addActionListener(this);

jia.addActionListener(this);

yu.addActionListener(this);

gen.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if (t.getText().equals("计算、结果_文本框,支持连续四则运算"))

t.setText("");

if (e.getActionCommand().equals("清除")) {//所有变量归0

t.setText("");

t1.setText("");

ysf = "";

l.setText("");

temp1 = 0;

}

if (e.getActionCommand().equals("Back")) {

StringBuffer s = new StringBuffer(t.getText());

if (s.length() != 0)

t.setText(s.delete(s.length() - 1, s.length()) + "");//删除最后一个字符

}

if (e.getActionCommand().equals("1")) {

t.setText(t.getText() + "1");

l.setText(l.getText() + "1");

}

if (e.getActionCommand().equals("2")) {

t.setText(t.getText() + "2");

l.setText(l.getText() + "2");

}

if (e.getActionCommand().equals("3")) {

t.setText(t.getText() + "3");

l.setText(l.getText() + "3");

}

if (e.getActionCommand().equals("4")) {

t.setText(t.getText() + "4");

l.setText(l.getText() + "4");

}

if (e.getActionCommand().equals("5")) {

t.setText(t.getText() + "5");

l.setText(l.getText() + "5");

}

if (e.getActionCommand().equals("6")) {

t.setText(t.getText() + "6");

l.setText(l.getText() + "6");

}

if (e.getActionCommand().equals("7")) {

t.setText(t.getText() + "7");

l.setText(l.getText() + "7");

}

if (e.getActionCommand().equals("8")) {

t.setText(t.getText() + "8");

l.setText(l.getText() + "8");

}

if (e.getActionCommand().equals("9")) {

t.setText(t.getText() + "9");

l.setText(l.getText() + "9");

}

if (e.getActionCommand().equals("0")) {

t.setText(t.getText() + "0");

l.setText(l.getText() + "0");

}

if (e.getActionCommand().equals(".")) {

t.setText(t.getText() + ".");

l.setText(l.getText() + ".");

}

if (e.getActionCommand().equals("+")) {

//不使用正则,另类连续四则运算思路:每次点击运算符先计算出之前所输数值,再用此结果再计算

if (ysf != "") {//判断是否为连续计算

go();//先计算出前一次结果

temp1 = r;//把结果赋给临时变量1

t.setText("");

t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");

} else {

temp1 = Float.parseFloat(t.getText());

t.setText("");

}

ysf = "+";

l.setText(l.getText() + "+");

}

if (e.getActionCommand().equals("-")) {

if (ysf != "") {

go();

temp1 = r;

t.setText("");

t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");

} else {

temp1 = Float.parseFloat(t.getText());

t.setText("");

}

ysf = "-";

l.setText(l.getText() + "-");

}

if (e.getActionCommand().equals("÷")) {

if (ysf != "") {

go();

temp1 = r;

t.setText("");

t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");

} else {

temp1 = Float.parseFloat(t.getText());

t.setText("");

}

ysf = "÷";

l.setText(l.getText() + "÷");

}

if (e.getActionCommand().equals("×")) {

if (ysf != "") {

go();

temp1 = r;

t.setText("");

t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");

} else {

temp1 = Float.parseFloat(t.getText());

t.setText("");

}

ysf = "×";

l.setText(l.getText() + "×");

}

if (e.getActionCommand().equals("√")) {

t1.setText("√" + t.getText());

t.setText(Math.sqrt(Double.parseDouble(t.getText())) + "");

}

if (e.getActionCommand().equals("%")) {

temp1 = Float.parseFloat(t.getText());

t.setText("");

ysf = "%";

}

if (e.getActionCommand().equals("=")) {

go();

t.setText(r + "");

t1.setText(temp1 + ysf + temp2 + "=" + t.getText());

ysf = "";

l.setText(l.getText() + "="+r);

}

}

void go() {//计算结果

temp2 = t.getText();

if (ysf.equals("+"))

r = temp1 + Float.parseFloat(temp2);

if (ysf.equals("-"))

r = temp1 - Float.parseFloat(temp2);

if (ysf.equals("×"))

r = temp1 * Float.parseFloat(temp2);

if (ysf.equals("÷"))

r = temp1 / Float.parseFloat(temp2);

if (ysf.equals("%"))

r = temp1 % Float.parseFloat(temp2);

}

}

java 连续运算_java 另类方法实现计算机连续四则运算相关推荐

  1. java比较运算_Java比较运算符

    注意哦: 1.  > . < . >= . <= 只支持左右两边操作数是数值类型 2.  == . != 两边的操作数既可以是数值类型,也可以是引用类型 public clas ...

  2. 学习笔记:JAVA大数运算(数组方法)

    提到大数运算,JAVA绝对赖皮,不像C,JAVA自带大整数(java.math.BigInteger)的库.这方面也是了解了一下,关于大整数,这一篇文章还是讲的很全面的:(6条消息) Java 大数字 ...

  3. java biginteger 运算_Java大数字运算之BigInteger 原创

    在 Java中,有许多数字处理的类,比如Integer 类.但是Integer 类有一定的局限性,下面我们就来看看比 Integer 类更厉害的一个,BigInteger类. BigInteger类型 ...

  4. java 对数运算_Java语言 第二章 运算和语句

    1.数据运算 运算由表达式表示,表达式由运算符和运算分量组成,运算分量可以是常量.变量和方法调用Java基本运算分类图 1.1 赋值运算 1.1.1 简单赋值运算变量 = 表达式 程序执行过程: (1 ...

  5. java float 运算_java float除法的问题

    展开全部 您好,1.整数的除法:32313133353236313431303231363533e78988e69d8331333337386536 0做除数抛运行时异常:两整数商会做取整运算,Flo ...

  6. java mod 运算_java中基本运算符

    java中的位运算符及其用法. java中的位运算符及其用法.请一一解释一下.包括例子 位逻辑运算符有"与"(AND)."或"(OR)."异或(XOR ...

  7. java日历教程_JAVA Calendar方法使用基础教程详解

    究竟什么是一个 Calendar 呢?中文的翻译就是日历,那我们立刻可以想到我们生活中有阳(公)历.阴(农)历之分.它们的区别在哪呢? 比如有: 月份的定义 – 阳`(公)历 一年12 个月,每个月的 ...

  8. java 静态方法覆盖_Java中方法的覆盖和静态方法的隐藏

    下面的程序对巴辛吉小鬣狗和其它狗之间的行为差异进行了建模.如果你不知道 什么是巴辛吉小鬣狗,那么我告诉你,这是一种产自非洲的小型卷尾狗,它们从 来都不叫唤.那么,这个程序将打印出什么呢? class ...

  9. java 重载 返回_java – 返回方法重载

    我是Java的新手,我自己也在学习.我尝试重载方法时遇到了麻烦.这是代码 public static void main(String[] args) { calculateScore(); calc ...

最新文章

  1. 一周一论文(翻译)——[VLDB 19] Minimizing Cost by Reducing Scaling Operators in Distributed Stream Processing
  2. POJ-1322 Chocolate 动态规划
  3. CI/CD with drone
  4. c语言什么是内联函数,C语言中内联函数inline的使用方法
  5. linux的nice命令用法,nice命令详解
  6. mysql 5.7 enum_MYSQL中 ENUM 类型的详细解释
  7. WPF学习之路(二) XAML(续)
  8. Linux DNS 服务器安装、配置和维护
  9. AdaBoost--从原理到实现
  10. 太漂亮了!有了3款开源图标库,不用再去求设计师了
  11. 内网服务器通过CCproxy代理上网
  12. 机器学习数据集划分留出法,留一法,交叉法,自助法
  13. 下单账户与实付账户不一致_如何保护您的不一致帐户
  14. IDEA 断点调试,为啥断点没有起作用
  15. MeeGo系统和SailFish系统_我是亲民_新浪博客
  16. 目标检测算法模型YOLOV3原理及其实战 课程简介
  17. 来去之间:微博第四季度净营收4.819亿美元 同比增长28%
  18. 出现The specified base path ““ contains a package but “catkin_make“ must be invoked...的解决方法
  19. 网络编程:空调维修系统
  20. Halcon 缺陷检测 PCB板检查(灰度开闭运算)

热门文章

  1. android锁屏(三)
  2. 026 [转载]冰血大哥的一篇学习计划
  3. 免费图床汇总(经常更新)
  4. 【编程语言 | C语言】C 语言编程规范
  5. PVE虚拟机篇-rest api
  6. 链表算法 Implemented with C++
  7. 安装Agile Controller
  8. Mysql添加新用户,并设置数据库权限
  9. 动手实现简易网站目录扫描器——WebScanner
  10. 真实案例教你玩转移动广告