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

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

代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//计算原理,将操作整合成字符串,先以加减分割字符串,对带有乘除的字符串先进行运算,在按加减对运算结果运算
 
class computer extends JFrame implements ActionListener, KeyListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String strin = "";
	JTextField t1 = new JTextField();
	char[] z = new char[100];
	int number = 0;
	int panduan = 0;
 
	computer() {
		this.setTitle("我的计算器1.0");
		this.setSize(270, 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.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e) {
		String lab = e.getActionCommand();
		if (lab.equals("1")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "1";
			t1.setText(strin);
		} else if (lab.equals("2")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "2";
			t1.setText(strin);
		} else if (lab.equals("3")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "3";
			t1.setText(strin);
		} else if (lab.equals("4")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "4";
			t1.setText(strin);
		} else if (lab.equals("5")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "5";
			t1.setText(strin);
		} else if (lab.equals("6")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "6";
			t1.setText(strin);
		} else if (lab.equals("7")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "7";
			t1.setText(strin);
		} else if (lab.equals("8")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "8";
			t1.setText(strin);
		} else if (lab.equals("9")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "9";
			t1.setText(strin);
		} else if (lab.equals("0")) {
			if(panduan==1)
			{
				strin="";
				panduan=0;
			}
			strin = strin + "0";
			t1.setText(strin);
		} else if (lab.equals("清零")) {
			strin = "";
			number = 0;
			t1.setText(strin);
		} else if (lab.equals("平方")) {
			if(panduan==1)
			{
				panduan=0;
			}
			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.equals("+")) {
			if(panduan==1)
			{
				panduan=0;
			}
			strin = strin + "+";
			z[number++] = '+';
			t1.setText(strin);
		} else if (lab.equals("-")) {
			if(panduan==1)
			{
				panduan=0;
			}
			strin = strin + "-";
			z[number++] = '-';
			t1.setText(strin);
		} else if (lab.equals("*")) {
			if(panduan==1)
			{
				panduan=0;
			}
			strin = strin + "*";
			t1.setText(strin);
		} else if (lab.equals("/")) {
			if(panduan==1)
			{
				panduan=0;
			}
			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;
				panduan=1;
			}
		}
	}
 
	public void keyPressed(KeyEvent e) {
	}
 
	public void keyReleased(KeyEvent e) {
	}
 
	double judge(String x) {
		if(x.equals(""))
			return 0;
		String[] y = x.split("\\*|/");
		double zo=Double.parseDouble(y[0]);
		int ci=1;
		for (int a = 0; a < x.length(); a++) {
			if (x.charAt(a) == '*') {
				zo=zo* Double.parseDouble(y[ci++]);
			} else if (x.charAt(a) == '/') {
				zo=zo/ Double.parseDouble(y[ci++]);
			}
		}
		return zo;
	}
	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();
	}
}

对于能改进的地方,和代码如何优化,欢迎各位在评论区指出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值