用JAVA编写一个计算器完成简单的加法、减法、除法和乘法操作.RT

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/19 07:25:43
用JAVA编写一个计算器完成简单的加法、减法、除法和乘法操作.RT

用JAVA编写一个计算器完成简单的加法、减法、除法和乘法操作.RT
用JAVA编写一个计算器完成简单的加法、减法、除法和乘法操作.
RT

用JAVA编写一个计算器完成简单的加法、减法、除法和乘法操作.RT

import java.applet.*;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

/


/import java.math.*;
public class cal extends Applet {
//定义所有需要使用的全局变量 String str="0";//暂存显示屏数据 String sign="null";//暂存符号数据 double num=0;//暂存内部运算数据
boolean change=false;//暂存内部四则运算起用 
GridLayout g0,g1;//定义AWT布局部件 JTextField number; 
Button num_p,num_0,num_1,num_2,num_3,num_4,num_5,num_6,num_7,num_8,num_9; Button act_add,act_mul,act_div,act_sub; Button mat_sin,mat_cos,mat_tan; Button do_ans,do_del; Panel p0,p1; 
public void init()//主程序部分 {
 //定义各个控件的样式
 number=new JTextField(20);//显示屏 
 number.setHorizontalAlignment(JTextField.RIGHT);   number.setEnabled(false);   number.setText("0");  
 num_p=new Button(".");//小数点的按钮   num_p.setForeground(Color.red); 
 num_p.addActionListener(new getbt());   
 num_0=new Button("0");//数字0的按钮   num_0.setForeground(Color.red); 
 num_0.addActionListener(new getbt());   
 num_1=new Button("1");//数字1的按钮   num_1.setForeground(Color.red); 
 num_1.addActionListener(new getbt());    
 num_2=new Button("2");//数字2的按钮   num_2.setForeground(Color.red); 






 num_2.addActionListener(new getbt());   
 num_3=new Button("3");//数字3的按钮   num_3.setForeground(Color.red); 
 num_3.addActionListener(new getbt());   
 num_4=new Button("4");//数字4的按钮   num_4.setForeground(Color.red); 
 num_4.addActionListener(new getbt());  
 num_5=new Button("5");//数字5的按钮   num_5.setForeground(Color.red); 
 num_5.addActionListener(new getbt());  
 num_6=new Button("6");//数字6的按钮   num_6.setForeground(Color.red); 
 num_6.addActionListener(new getbt());   
 num_7=new Button("7");//数字7的按钮   num_7.setForeground(Color.red); 
 num_7.addActionListener(new getbt());  
 num_8=new Button("8");//数字8的按钮   num_8.setForeground(Color.red); 
 num_8.addActionListener(new getbt());  
 num_9=new Button("9");//数字9的按钮   num_9.setForeground(Color.red); 
 num_9.addActionListener(new getbt());  
 act_add=new Button("+");//加法的按钮   act_add.setForeground(Color.red); 
 act_add.addActionListener(new getbt());   
 act_sub=new Button("-");//减法的按钮   act_sub.setForeground(Color.red); 
 act_sub.addActionListener(new getbt());  
 act_mul=new Button("*");//乘法的按钮   act_mul.setForeground(Color.red); 
 act_mul.addActionListener(new getbt());  
 act_div=new Button("/");//除法的按钮   act_div.setForeground(Color.red); 






 act_div.addActionListener(new getbt());   
 do_ans=new Button("=");//等于的按钮   do_ans.setForeground(Color.red); 
 do_ans.addActionListener(new getbt());  
 do_del=new Button("del");//等于的按钮   do_del.setForeground(Color.red); 
 do_del.addActionListener(new getbt());   
 mat_sin=new Button("sin");//sin的按钮   mat_sin.setForeground(Color.red); 
 mat_sin.addActionListener(new getbt());   
 mat_cos=new Button("cos");//cos的按钮   mat_cos.setForeground(Color.red); 
 mat_cos.addActionListener(new getbt());   
 mat_tan=new Button("tan");//tan的按钮   mat_tan.setForeground(Color.red); 
 mat_tan.addActionListener(new getbt());     
 //布局整个计算器
 g0=new GridLayout(1,1,10,10);//布局输入输出大体位置  g1=new GridLayout(4,5,10,10);  
 p0=new Panel(); //显示屏的布局  p0.setLayout(g0);  p0.add(number); 
 p0.setBounds(0,0,100,50);   
 //按纽的布局   p1=new Panel();  p1.setLayout(g1);
 p1.add(num_1);//第一排按纽  p1.add(num_2);  p1.add(num_3);  p1.add(act_add);  p1.add(act_sub);  
 p1.add(num_4);//第二排按纽






 p1.add(num_5);  p1.add(num_6);  p1.add(act_mul);  p1.add(act_div);  
 p1.add(num_7);//第三排按纽  p1.add(num_8);  p1.add(num_9);  p1.add(mat_sin);  p1.add(mat_cos);  
 p1.add(num_0);//第四排按钮  p1.add(num_p);  p1.add(do_ans);  p1.add(do_del);  p1.add(mat_tan);
 p1.setBounds(5,60,400,250); 
 p1.setBackground(Color.cyan);//设置按钮背景颜色  
 setLayout(null);   add(p0);  add(p1);  
 this.setBackground(Color.cyan);//设置背景整体颜色 } 
class getbt implements ActionListener //监听作出反应的类 {
 public void actionPerformed(ActionEvent e)//对每个每个按钮做出响应  { 
 if(e.getSource()==num_0)   press0();
 else if(e.getSource()==num_1)   press1();
 else if(e.getSource()==num_2)   press2();
 else if(e.getSource()==num_3)   press3();
 else if(e.getSource()==num_4)   press4();
 else if(e.getSource()==num_5)   press5();
 else if(e.getSource()==num_6)   press6();






 else if(e.getSource()==num_7)   press7();
 else if(e.getSource()==num_8)   press8();
 else if(e.getSource()==num_9)   press9();
 else if(e.getSource()==num_p)   pressp();
 else if(e.getSource()==act_add)   pressadd();
 else if(e.getSource()==act_mul)   pressmul();
 else if(e.getSource()==act_div)   pressdiv();
 else if(e.getSource()==act_sub)   presssub();
 else if(e.getSource()==mat_sin)   presssin();
 else if(e.getSource()==mat_tan)   presstan();
 else if(e.getSource()==mat_cos)   presscos();
 else if(e.getSource()==do_ans)   pressans();
 else if (e.getSource()==do_del)   pressdel();  }    }
public void press0()//对按0作出反应 {
 if(str.length()<16 && str!="0" && change==false)  {
  str+="0";  }
 else if(str=="0" || sign!="null")  {
  str="0";  }
 number.setText(str); } 
public void press1()//对按1作出反应 {




































用JAVA编写一个计算器完成简单的加法、减法、除法和乘法操作.RT Java编写计算器的流程图 用java编写计算器 能够实现简单的加、减、乘、除、乘方、开方运算等; 用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算. 如何编写一个打印50~100随机数的简单的程序用java写 用java语言编写一个简单计算器,并要实验报告和步骤说明.计算器的版样要好看,背景颜色好看点吧```报告和步骤说明要详细.谢谢大虾了```计算器的版样要好看,背景颜色好看点吧```报告和步骤 编写一个简易的计算器. 我做了一个android计算器,三角函数和根号要怎么实现啊,求代码,在eclipse里面编写的,一些直接JAVA的貌似用不了 编写一个Java应用程序,计算1-1/2+1/3-1/4+…的前100项之和.用简单的JAVA写出来 编写一个模拟计算器的程序.要包括以下方法:加法,减法,乘法,除法.每个方法至少有两个参数. 采用java语言编写一个计算器,该计算器能对正数、负数(这里的正数、负数包括小数)进行加、减、乘、除运 如何用vc++编写一个简单的(只有加减乘除)计算器程序? 如何使用Java对象语言编写一个加减乘除计算器要有代码 C#编写简单计算器简单计算器:完成简单的计算器,可以进行(+,-,*,/,^(乘方),sqrt(平方根))的算术运算.+,-,*,/,^(乘方)是二元运算(需要2个操作数),sqrt为一元运算.对于一元运算:输入数 用java编写一个计算矩形面积和立方体体积的类,该类完成计算的方法用静态方法实现如题 老师要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加减乘除的运算.java C语言程序设计,做一个简单计算器程序内容有:加减乘除和平方,立方,开方等.用if或者swith结构来编程.算加法时只输出加法的结果,算减法时只出减法的结果,如此类推. 用C#编写一个简单的计算器,只含有加减乘除四则运算,怎么处理被除数不能为零的异常呢