栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA简易计算器实现(学校作业)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA简易计算器实现(学校作业)

学校要求的界面,没有等号我不是很理解,本计算器在文本框上打回车代替等号

代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//计算原理,将操作整合成字符串,先以加减分割字符串,对带有乘除的字符串先进行运算,在按加减对运算结果运算

class computer extends JFrame implements ActionListener, KeyListener {
	String strin = "";
	JTextField t1 = new JTextField();
	char[] z = new char[100];
	int number = 0;

	computer() {
		this.setTitle("我的计算器1.0");
		this.setSize(260, 215);
		JPanel p1 = new JPanel();
		p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
		JLabel L1 = new JLabel("计算结果:");
		t1.addKeyListener(this);
		p1.add(L1);
		p1.add(t1);
		add(p1, BorderLayout.NORTH);
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(4, 3));
		JPanel p4 = new JPanel();
		p4.setLayout(new FlowLayout());
		JButton[] x = new JButton[12];
		x[0] = new JButton("1");
		x[1] = new JButton("2");
		x[2] = new JButton("3");
		x[3] = new JButton("4");
		x[4] = new JButton("5");
		x[5] = new JButton("6");
		x[6] = new JButton("7");
		x[7] = new JButton("8");
		x[8] = new JButton("9");
		x[9] = new JButton("0");
		x[10] = new JButton("清零");
		x[11] = new JButton("平方");
		for (int a = 0; a < 12; a++)
			p2.add(x[a]);
		p4.add(p2);
		JPanel p3 = new JPanel();
		p3.setLayout(new GridLayout(4, 1));
		JButton[] y = new JButton[4];
		y[0] = new JButton("+");
		y[1] = new JButton("-");
		y[2] = new JButton("*");
		y[3] = new JButton("/");
		for (int a = 0; a < 4; a++)
			p3.add(y[a]);
		p4.add(p3);
		add(p4, BorderLayout.CENTER);
		JLabel l2 = new JLabel("版权所有:zzuli", SwingConstants.RIGHT);
		add(l2, BorderLayout.SOUTH);
		for (int i = 0; i < 12; i++) {
			x[i].addActionListener(this);
		}
		for (int i = 0; i < 4; i++) {
			y[i].addActionListener(this);
		}
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		String lab = e.getActionCommand();
		if (lab == "1") {
			strin = strin + "1";
			t1.setText(strin);
		} else if (lab == "2") {
			strin = strin + "2";
			t1.setText(strin);
		} else if (lab == "3") {
			strin = strin + "3";
			t1.setText(strin);
		} else if (lab == "4") {
			strin = strin + "4";
			t1.setText(strin);
		} else if (lab == "5") {
			strin = strin + "5";
			t1.setText(strin);
		} else if (lab == "6") {
			strin = strin + "6";
			t1.setText(strin);
		} else if (lab == "7") {
			strin = strin + "7";
			t1.setText(strin);
		} else if (lab == "8") {
			strin = strin + "8";
			t1.setText(strin);
		} else if (lab == "9") {
			strin = strin + "9";
			t1.setText(strin);
		} else if (lab == "0") {
			strin = strin + "0";
			t1.setText(strin);
		} else if (lab == "清零") {
			strin = "";
			number = 0;
			t1.setText(strin);
		} else if (lab == "平方") {
			try {
			String p="";
			int a=strin.length()-1;
			for(;a>=0;a--)
			{
				if(strin.charAt(a)=='+'||strin.charAt(a)=='-'||strin.charAt(a)=='*'||strin.charAt(a)=='/')
					break;
			}
			p=strin.substring(a+1);
			Double f= Double.parseDouble(p);
			f=f*f;
			if(a==0&&(strin.charAt(a)=='+'||strin.charAt(a)=='-'))
				strin=strin.substring(1, a+1)+f;
			else
			strin=strin.substring(0, a+1)+f;
			t1.setText(strin);
			}catch (Exception q) {
				t1.setText("格式错误");
				strin="";
			}
		} else if (lab == "+") {
			strin = strin + "+";
			z[number++] = '+';
			t1.setText(strin);
		} else if (lab == "-") {
			strin = strin + "-";
			z[number++] = '-';
			t1.setText(strin);
		} else if (lab == "*") {
			strin = strin + "*";
			t1.setText(strin);
		} else if (lab == "/") {
			strin = strin + "/";
			t1.setText(strin);
		}
	}

	public void keyTyped(KeyEvent e) {
		if ((char) e.getKeyChar() == KeyEvent.VK_ENTER) {
			String str1 = strin;
			String[] x = str1.split("\+|-");
			try {
				judge1(str1);
				double[] y = new double[x.length];
				for (int a = 0; a < x.length; a++) {
					y[a] = judge(x[a]);
				}
				if (x.length == number) {
					if (z[0] == '-')
						y[0] = -y[0];
					for (int a = 1; a < x.length; a++) {
						if (z[a] == '+')
							y[0] = y[0] + y[a];
						else
							y[0] = y[0] - y[a];
					}
				} else {
					for (int a = 1; a < x.length; a++) {
						if (z[a - 1] == '+')
							y[0] = y[0] + y[a];
						else
							y[0] = y[0] - y[a];
					}
				}
				strin="" + y[0];
				t1.setText(strin);
			} catch (Exception q) {
				t1.setText("格式错误");
				strin="";
			} finally {
				number=0;
			}
		}
	}

	public void keyPressed(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}

	double judge(String x) {
		for (int a = 0; a < x.length(); a++) {
			if (x.charAt(a) == '*') {
				String[] y = x.split("\*");
				return Double.parseDouble(y[0]) * Double.parseDouble(y[1]);
			} else if (x.charAt(a) == '/') {
				String[] y = x.split("/");
				return Double.parseDouble(y[0]) / Double.parseDouble(y[1]);
			}
		}
		return Double.parseDouble(x);
	}
	void judge1(String x) throws Exception{
		if(x.endsWith("+")||x.endsWith("-")||x.endsWith("*")||x.endsWith("/"))
			throw new Exception();
	}
}

public class hello {
	public static void main(String[] args) {
		new computer();
	}
}
对于能改进的地方,和代码如何优化,欢迎各位在评论区指出
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821214.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号