当前位置:   article > 正文

JAVA简单计算器乘法_乘法计算器java

乘法计算器java
package work1;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntToDoubleFunction;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class TestMain {
	JFrame jFrame;//成员变量,未设置访问类型,默认包内访问,先创建一个JF对象
	String stringx;//保存第一个变量
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		TestMain testMain=new TestMain();
		testMain.initView();
	}
	public void initView() {
		jFrame=new JFrame("好好学习");//第一步,初始化
		jFrame.setBounds(500, 500, 600, 600);//第二步,设置位置大小
		jFrame.setLayout(null);//控制布局 null为不使用系统所给的任何一种布局
		final JTextField jTextField=new JTextField();
		jTextField.setBounds(50, 50,500, 50);
		jFrame.add(jTextField);
		
		
		//数字符******************************************************************
		JButton jButton0=new JButton("0");//********00000000000000000
		jButton0.setBounds(50, 100, 50, 50);
		jFrame.add(jButton0);
		jButton0.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string =jTextField.getText().trim();//去掉两端空格的代码trim
				if(!string.equals("0"))
				{
					string=string+"0";
					jTextField.setText(string);
				}
			}
		});
		
		JButton jButton1=new JButton("1");//**********111111111111111111111111
		jButton1.setBounds(100, 100, 50, 50);
		jFrame.add(jButton1);
		jButton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string= jTextField.getText().trim();
				if(string.equals("0"))
				{
					jTextField.setText("1");
				}
				else
				{
					string=string+"1";
					jTextField.setText(string);
				}
			}
			
		})	;
		JButton jButton2=new JButton("2");
		jButton2.setBounds(150, 100, 50, 50);
		jFrame.add(jButton2);
		jButton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string= jTextField.getText().trim();
				if(string.equals("0"))
				{
					jTextField.setText("2");
				}
				else
				{
					string=string+"2";
					jTextField.setText(string);
				}
			}
			
		})	;
		//数字符***************************数字符***************************************
		
		
		
		//运算符***************************运算符***************************************
		JButton jButton11=new JButton("+");
		jButton11.setBounds(150, 150, 50, 50);
		jButton11.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				jTextField.setText(jTextField.getText()+"+");
			}
		});
		jFrame.add(jButton11);
		//运算符***************************运算符***************************************
		
		
		
		//操作符***************************操作符***************************************
		JButton jButtonC=new JButton("C");//清零
		jButtonC.setBounds(50, 150, 50, 50);
		jButtonC.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonC);//................................
		JButton jButtonD=new JButton(".");//加点
		jButtonD.setBounds(100,150, 50, 50);
		jButtonD.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
			String string=jTextField.getText();
			if(!string.contains("."))
			{
				
				string=string+".";
			}
			}
		});
		jFrame.add(jButtonD);
		
		
		JButton jButtonE=new JButton("=");//结果
		jButtonE.setBounds(200,150, 50, 50);
		jButtonE.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
			ArrayList<Integer> op=new ArrayList<Integer>();
			ArrayList<Double> sum=new ArrayList<Double>();
			String string=jTextField.getText()+"+";
			double num1=0;//乘除号前面的值
			int mark=0;//乘除标志词
			double t=0;//每个数的值
			
			int m=Integer.valueOf('*');
			int n=Integer.valueOf('/');
			int a=Integer.valueOf('0');
			int b=Integer.valueOf('9');
			int ad=Integer.valueOf('+');
			for(int i=0;i<string.length();i++)
			{
				int num=Integer.valueOf(string.charAt(i));
				//将i的字符转化为asc码
				if(a<=num&&num<=b)
					{
						t=t*10+(num-a);
					}
				else
					{
					if(mark!=0)
						t=(mark==1)?(t*num1):(t/num1);//求出乘除运算结果赋值给t
					
					if(num==m)
						{
								num1=t;
								mark=1;
								
						}//如果后面是*,则标记乘号,将乘号前的那个数赋值给num1保存
					else 
						if(num==n)
						{
								num1=t;
								mark=2;	
								
						}
					
						else {//加减 则保存
							mark=0;
							op.add(num);//存符号
							sum.add(t);//存数据
							
						}
					t=0;
					}
			
			}
			
			double ans=sum.get(0);//答案 4 2
			for(int i=1;i<sum.size();i++)
			{
				Double num2=sum.get(i);
				int op1=op.get(i-1);
				ans=(op1==ad)?(ans+num2):(ans-num2);
			}
			
			jTextField.setText(Double.toString(ans));
			}
			
			
		});
		jFrame.add(jButtonE);
		
		jFrame.setVisible(true);//第三部,让界面显示出来,这句话一定在最后
		//但凡是成对的元素,必须要先写出来再将光标移到中间写其他的代码
	}

}
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/67488
推荐阅读
相关标签
  

闽ICP备14008679号