然后 通过输入 显示结果,比如说:
以下是上图计算器的代码:
package Computer;
import javaawtBorderLayout;
import javaawtColor;
import javaawtContainer;
import javaawtFont;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javautilStack;
import javaxswingJApplet;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJTextField;
public class Count extends JApplet implements ActionListener
{
/
/
private static final long serialVersionUID = 1L;
private JTextField textField = new JTextField("请输入");
String operator = "";// *** 作
String input = "";//输入的 式子
boolean flag = true;
// boolean flag1 = true;
// boolean flag2 = true;
public void init()//覆写Applet里边的init方法
{
Container C = getContentPane();
JButton b[] = new JButton[16];
JPanel panel = new JPanel();
Cadd(textField, BorderLayoutNORTH);
Cadd(panel,BorderLayoutCENTER);
panelsetLayout(new GridLayout(4, 4,5,5));
String name[]={"7","8","9","+","4","5","6","-","1","2","3","","0","C","=","/"};//设置 按钮
for(int i=0;i<16;i++)//添加按钮
{
b[i] = new JButton(name[i]);
b[i]setBackground(new Color(192,192,192));
b[i]setForeground(ColorBLUE);//数字键 设置为 蓝颜色
if(i%4==3)
b[i]setForeground(ColorRED);
b[i]setFont(new Font("宋体",FontPLAIN,16));//设置字体格式
paneladd(b[i]);
b[i]addActionListener(this);
}
b[13]setForeground(ColorRED);//非数字键,即运算键设置为红颜色
b[13]setForeground(ColorRED);
}
public void actionPerformed(ActionEvent e)
{
int cnt = 0;
String actionCommand = egetActionCommand();
if(actionCommandequals("+")||actionCommandequals("-")||actionCommandequals("") ||actionCommandequals("/"))
input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子
else if(actionCommandequals("C"))
input = "";
else if(actionCommandequals("="))//当监听到等号时,则处理 input
{
input+= "="+compute(input);
textFieldsetText(input);
input="";
cnt = 1;
}
else
input += actionCommand;//数字为了避免多位数的输入 不需要加空格
if(cnt==0)
textFieldsetText(input);
}
private String compute(String input)//即1237 的 样例
{
String str[];
str = inputsplit(" ");
Stack<Double> s = new Stack<Double>();
double m = DoubleparseDouble(str[0]);
spush(m);
for(int i=1;i<strlength;i++)
{
if(i%2==1)
{
if(str[i]compareTo("+")==0)
{
double help = DoubleparseDouble(str[i+1]);
spush(help);
}
if(str[i]compareTo("-")==0)
{
double help = DoubleparseDouble(str[i+1]);
spush(-help);
}
if(str[i]compareTo("")==0)
{
double help = DoubleparseDouble(str[i+1]);
double ans = speek();//取出栈顶元素
spop();//消栈
ans=help;
spush(ans);
}
if(str[i]compareTo("/")==0)
{
double help = DoubleparseDouble(str[i+1]);
double ans = speek();
spop();
ans/=help;
spush(ans);
}
}
}
double ans = 0d;
while(!sisEmpty())
{
ans+=speek();
spop();
}
String result = StringvalueOf(ans);
return result;
}
public static void main(String args[])
{
JFrame frame = new JFrame("Count");
Count applet = new Count();
framegetContentPane()add(applet, BorderLayoutCENTER);
appletinit();//applet的init方法
appletstart();//线程开始
framesetSize(350, 400);//设置窗口大小
framesetVisible(true);//设置窗口可见
}
}
import javautil;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(Systemin);
Systemoutprintln("简易计算器");
Systemoutprintln("\t\t\t\t\t");
Systemoutprintln(" 使用说明: 1加法 2减法 3乘法 4除法 ");
Systemoutprintln("\t\t\t\t\t");
Systemoutprintln("");
for(int i=0;i<100;i++){
Systemoutprint("\n请选择运算规则:");
int num = inputnextInt();
switch(num){
case 1:
Systemoutprintln("\n你选择了加法\n");
Systemoutprint("请输入第1个加数:");
int jiashu1 = inputnextInt();
Systemoutprint("请输入第2个加数:");
int jiashu2 = inputnextInt();
Systemoutprintln("运算结果为:" + jiashu1 + " + " + jiashu1 + " = " + (jiashu1 + jiashu2));
break;
case 2:
Systemoutprintln("\n你选择了减法\n");
Systemoutprint("请输入被减数:");
int jianshu1 = inputnextInt();
Systemoutprint("请输入减数:");
int jianshu2 = inputnextInt();
Systemoutprintln("运算结果为:" + jianshu1 + " - " + jianshu2 + " = " + (jianshu1 - jianshu2));
break;
case 3:
Systemoutprintln("\n你选择了乘法\n");
Systemoutprint("请输入第1个因数:");
int chengfa1 = inputnextInt();
Systemoutprint("请输入第2个因数:");
int chengfa2 = inputnextInt();
Systemoutprintln("运算结果为:" + chengfa1 + " " + chengfa2 + " = " + (chengfa1 chengfa2));
break;
case 4:
Systemoutprintln("\n你选择了除法\n");
Systemoutprint("请输入被除数:");
double chufa1 = inputnextInt();
Systemoutprint("请输入除数:");
double chufa2 = inputnextInt();
Systemoutprintln("运算结果为:" + chufa1 + " / " + chufa2 + " = " + (chufa1 / chufa2) + " 余 " + (chufa1 % chufa2));
break;
default:
Systemoutprintln("\n你的选择有错,请重新选择!");
break;
}
}
}
}
import javaawt;
import javaawtevent;
import javalang;
import javaxswing;
public class Counter extends Frame
{
//声明三个面板的布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//显示屏所显示的字符串
double x,y;//x和y都是运算数
int z;//Z表示单击了那一个运算符0表示"+",1表示"-",2表示"",3表示"/"
static double m;//记忆的数字
public Counter()
{
gl1=new GridLayout(1,4,10,0);//实例化三个面板的布局
gl2=new GridLayout(4,1,0,15);
gl3=new GridLayout(4,5,10,15);
tf1=new JTextField(27);//显示屏
tf1setHorizontalAlignment(JTextFieldRIGHT);
tf1setEnabled(false);
tf1setText("0");
tf2=new TextField(10);//显示记忆的索引值
tf2setEditable(false);
//实例化所有按钮、设置其前景色并注册监听器
b0=new Button("Backspace");
b0setForeground(Colorred);
b0addActionListener(new Bt());
b1=new Button("CE");
b1setForeground(Colorred);
b1addActionListener(new Bt());
b2=new Button("C");
b2setForeground(Colorred);
b2addActionListener(new Bt());
b3=new Button("MC");
b3setForeground(Colorred);
b3addActionListener(new Bt());
b4=new Button("MR");
b4setForeground(Colorred);
b4addActionListener(new Bt());
b5=new Button("MS");
b5setForeground(Colorred);
b5addActionListener(new Bt());
b6=new Button("M+");
b6setForeground(Colorred);
b6addActionListener(new Bt());
b7=new Button("7");
b7setForeground(Colorblue);
b7addActionListener(new Bt());
b8=new Button("8");
b8setForeground(Colorblue);
b8addActionListener(new Bt());
b9=new Button("9");
b9setForeground(Colorblue);
b9addActionListener(new Bt());
b10=new Button("/");
b10setForeground(Colorred);
b10addActionListener(new Bt());
b11=new Button("sqrt");
b11setForeground(Colorblue);
b11addActionListener(new Bt());
b12=new Button("4");
b12setForeground(Colorblue);
b12addActionListener(new Bt());
b13=new Button("5");
b13setForeground(Colorblue);
b13addActionListener(new Bt());
b14=new Button("6");
b14setForeground(Colorblue);
b14addActionListener(new Bt());
b15=new Button("");
b15setForeground(Colorred);
b15addActionListener(new Bt());
b16=new Button("%");
b16setForeground(Colorblue);
b16addActionListener(new Bt());
b17=new Button("1");
b17setForeground(Colorblue);
b17addActionListener(new Bt());
b18=new Button("2");
b18setForeground(Colorblue);
b18addActionListener(new Bt());
b19=new Button("3");
b19setForeground(Colorblue);
b19addActionListener(new Bt());
b20=new Button("-");
b20setForeground(Colorred);
b20addActionListener(new Bt());
b21=new Button("1/X");
b21setForeground(Colorblue);
b21addActionListener(new Bt());
b22=new Button("0");
b22setForeground(Colorblue);
b22addActionListener(new Bt());
b23=new Button("+/-");
b23setForeground(Colorblue);
b23addActionListener(new Bt());
b24=new Button("");
b24setForeground(Colorblue);
b24addActionListener(new Bt());
b25=new Button("+");
b25setForeground(Colorred);
b25addActionListener(new Bt());
b26=new Button("=");
b26setForeground(Colorred);
b26addActionListener(new Bt());
//实例化四个面板
p0=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
//创建一个空字符串缓冲区
str=new StringBuffer();
//添加面板p0中的组件和设置其在框架中的位置和大小
p0add(tf1);
p0setBounds(10,25,300,40);
//添加面板p1中的组件和设置其在框架中的位置和大小
p1setLayout(gl1);
p1add(tf2);
p1add(b0);
p1add(b1);
p1add(b2);
p1setBounds(10,65,300,25);
//添加面板p2中的组件并设置其的框架中的位置和大小
p2setLayout(gl2);
p2add(b3);
p2add(b4);
p2add(b5);
p2add(b6);
p2setBounds(10,110,40,150);
//添加面板p3中的组件并设置其在框架中的位置和大小
p3setLayout(gl3);//设置p3的布局
p3add(b7);
p3add(b8);
p3add(b9);
p3add(b10);
p3add(b11);
p3add(b12);
p3add(b13);
p3add(b14);
p3add(b15);
p3add(b16);
p3add(b17);
p3add(b18);
p3add(b19);
p3add(b20);
p3add(b21);
p3add(b22);
p3add(b23);
p3add(b24);
p3add(b25);
p3add(b26);
p3setBounds(60,110,250,150);
//设置框架中的布局为空布局并添加4个面板
setLayout(null);
add(p0);
add(p1);
add(p2);
add(p3);
setResizable(false);//禁止调整框架的大小
//匿名类关闭窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1)
{
Systemexit(0);
}
});
setBackground(ColorlightGray);
setBounds(100,100,320,280);
setVisible(true);
}
//构造监听器
class Bt implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
try{
if(e2getSource()==b1)//选择"CE"清零
{
tf1setText("0");//把显示屏清零
strsetLength(0);//清空字符串缓冲区以准备接收新的输入运算数
}
else if(e2getSource()==b2)//选择"C"清零
{
tf1setText("0");//把显示屏清零
strsetLength(0);
}
else if(e2getSource()==b23)//单击"+/-"选择输入的运算数是正数还是负数
{
x=DoubleparseDouble(tf1getText()trim());
tf1setText(""+(-x));
}
else if(e2getSource()==b25)//单击加号按钮获得x的值和z的值并清空y的值
{
x=DoubleparseDouble(tf1getText()trim());
strsetLength(0);//清空缓冲区以便接收新的另一个运算数
y=0d;
z=0;
}
else if(e2getSource()==b20)//单击减号按钮获得x的值和z的值并清空y的值
{
x=DoubleparseDouble(tf1getText()trim());
strsetLength(0);
y=0d;
z=1;
}
else if(e2getSource()==b15)//单击乘号按钮获得x的值和z的值并清空y的值
{
x=DoubleparseDouble(tf1getText()trim());
strsetLength(0);
y=0d;
z=2;
}
else if(e2getSource()==b10)//单击除号按钮获得x的值和z的值并空y的值
{
x=DoubleparseDouble(tf1getText()trim());
strsetLength(0);
y=0d;
z=3;
}
else if(e2getSource()==b26)//单击等号按钮输出计算结果
{
strsetLength(0);
switch(z)
{
case 0 : tf1setText(""+(x+y));break;
case 1 : tf1setText(""+(x-y));break;
case 2 : tf1setText(""+(xy));break;
case 3 : tf1setText(""+(x/y));break;
}
}
else if(e2getSource()==b24)//单击""按钮输入小数
{
if(tf1getText()trim()indexOf(′′)!=-1)//判断字符串中是否已经包含了小数点
{
}
else//如果没数点有小
{
if(tf1getText()trim()equals("0"))//如果初时显示为0
{
strsetLength(0);
tf1setText((strappend("0"+e2getActionCommand()))toString());
}
else if(tf1getText()trim()equals(""))//如果初时显示为空则不做任何 *** 作
{
}
else
{
tf1setText(strappend(e2getActionCommand())toString());
}
}
y=0d;
}
else if(e2getSource()==b11)//求平方根
{
x=DoubleparseDouble(tf1getText()trim());
tf1setText("数字格式异常");
if(x<0)
tf1setText("负数没有平方根");
else
tf1setText(""+Mathsqrt(x));
strsetLength(0);
y=0d;
}
else if(e2getSource()==b16)//单击了"%"按钮
{
x=DoubleparseDouble(tf1getText()trim());
tf1setText(""+(001x));
strsetLength(0);
y=0d;
}
else if(e2getSource()==b21)//单击了"1/X"按钮
{
x=DoubleparseDouble(tf1getText()trim());
if(x==0)
{
tf1setText("除数不能为零");
}
else
{
tf1setText(""+(1/x));
}
strsetLength(0);
y=0d;
}
else if(e2getSource()==b3)//MC为清除内存
{
m=0d;
tf2setText("");
strsetLength(0);
}
else if(e2getSource()==b4)//MR为重新调用存储的数据
{
if(tf2getText()trim()!="")//有记忆数字
{
tf1setText(""+m);
}
}
else if(e2getSource()==b5)//MS为存储显示的数据
{
m=DoubleparseDouble(tf1getText()trim());
tf2setText("M");
tf1setText("0");
strsetLength(0);
}
else if(e2getSource()==b6)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR
{
m=m+DoubleparseDouble(tf1getText()trim());
}
else//选择的是其他的按钮
{
if(e2getSource()==b22)//如果选择的是"0"这个数字键
{
if(tf1getText()trim()equals("0"))//如果显示屏显示的为零不做 *** 作
{
}
else
{
tf1setText(strappend(e2getActionCommand())toString());
y=DoubleparseDouble(tf1getText()trim());
}
}
else if(e2getSource()==b0)//选择的是“BackSpace”按钮
{
if(!tf1getText()trim()equals("0"))//如果显示屏显示的不是零
{
if(strlength()!=1)
{
tf1setText(strdelete(strlength()-1,strlength())toString());//可能抛出字符串越界异常
}
else
{
tf1setText("0");
strsetLength(0);
}
}
y=DoubleparseDouble(tf1getText()trim());
}
else//其他的数字键
{
tf1setText(strappend(e2getActionCommand())toString());
y=DoubleparseDouble(tf1getText()trim());
}
}
}
catch(NumberFormatException e){
tf1setText("数字格式异常");
}
catch(StringIndexOutOfBoundsException e){
tf1setText("字符串索引越界");
}
}
}
public static void main(String args[])
{
new Counter();
}
}
以上就是关于用JAVA编写一个简单的计算器,要求如下:全部的内容,包括:用JAVA编写一个简单的计算器,要求如下:、用java语言编写一个计算器的程序、Java中计算器的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)