java做一个计算器,请各位伸出援助之手,小女子感激不尽

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java做一个计算器,请各位伸出援助之手,小女子感激不尽相关的知识,希望对你有一定的参考价值。

1.所交程序代码长度不能少于100行。
2.程序必须能运行。
3.必须附加程序说明
4.程序说明格式如下:
A本程序用到了JAVA中的哪些技术,实现了哪些功能,有哪些还没有实现。
B 本程序包括有哪些类,每个类的主要功能是什么?每个类提供了哪些接口供用户使用
做一个计算器能实现加减乘除就可以了`

这是我的,希望对大家有帮助。
import javax.swing.*;
//import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

//计算器显示结果的窗体
class Result extends JPanel

JTextField text = new JTextField("0"); //text先是输入和结果
Result()


text.setHorizontalAlignment(SwingConstants.RIGHT); //讲输入的数字或得到的结果在text的右边显示
text.setEnabled(false); //文本框不能编辑
setLayout(new BorderLayout()); //设定布局管理器边框布局
add(text, BorderLayout.CENTER); //text放置在窗体的中间



//计算器数字按钮定义面板
class Number_Key extends JPanel

JButton zero = new JButton("0");//数字键0
JButton one = new JButton("1");//数字键1
JButton two = new JButton("2");//数字键2
JButton three = new JButton("3");//数字键3
JButton four = new JButton("4");//数字键4
JButton five = new JButton("5");//数字键5
JButton six = new JButton("6");//数字键6
JButton seven = new JButton("7");//数字键7
JButton eight = new JButton("8");//数字键8
JButton nine = new JButton("9");//数字键9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("ce");//置零键
JButton point = new JButton(".");
JButton tzero = new JButton("00");
Number_Key()

setLayout(new GridLayout(6, 3, 10, 10));//定义布局管理器为网格布局
//添加各个按钮键
add(seven);
add(eight);
add(nine);
add(four);
add(five);
add(six);
add(one);
add(two);
add(three);
add(zero);
add(tzero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);




//计算器主类
class sakura extends JFrame implements ActionListener

Result result = new Result();//定义text的面板
Number_Key number_key = new Number_Key();//定义按钮面板
//当点击按钮+、-、*、/时,com = true
boolean com = false;
//当i=0时说明是我们第一次输入,字符串sum不会累加
int i = 0;
//存放text的内容
String sum = "";
//存放点击按钮+、-、*、/之前的数值
double total = 0;
//+、-、*、/的代号分别为1,2,3,4
int symbol = 0;
sakura()

super("Calculator");//设定标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗体时退出程序
JPanel pane = new JPanel();//定义主面板
pane.setLayout(new BorderLayout());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.SOUTH);
number_key.one.addActionListener(this);//对1按钮添加监听事件
number_key.two.addActionListener(this);//对2按钮添加监听事件
number_key.three.addActionListener(this);//对3按钮添加监听事件
number_key.four.addActionListener(this);//对4按钮添加监听事件
number_key.five.addActionListener(this);//对5按钮添加监听事件
number_key.six.addActionListener(this);//对6按钮添加监听事件
number_key.seven.addActionListener(this);//对7按钮添加监听事件
number_key.eight.addActionListener(this);//对8按钮添加监听事件
number_key.nine.addActionListener(this);//对9按钮添加监听事件
number_key.zero.addActionListener(this);//对0按钮添加监听事件
number_key.ce.addActionListener(this);//对置零按钮添加监听事件
number_key.plus.addActionListener(this);//对+按钮添加监听事件
number_key.equal.addActionListener(this);//对=按钮添加监听事件
number_key.sub.addActionListener(this);//对-按钮添加监听事件
number_key.mul.addActionListener(this);//对*按钮添加监听事件
number_key.div.addActionListener(this);//对/按钮添加监听事件
number_key.tzero.addActionListener(this);//对00按钮添加监听事件
number_key.point.addActionListener(this);//对.按钮添加监听事件
setContentPane(pane);
pack();//初始化窗体大小为正好盛放所有按钮

//各个按钮触发的事件
public void actionPerformed(ActionEvent e)
/*如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true
* 如果没有com= false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
**/
if (e.getSource() == number_key.one)

if (com || i == 0)

result.text.setText("1");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "1");


else if (e.getSource() == number_key.two)

if (com || i == 0)

result.text.setText("2");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "2");


else if (e.getSource() == number_key.three)

if (com || i == 0)

result.text.setText("3");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "3");


else if (e.getSource() == number_key.four)

if (com || i == 0)

result.text.setText("4");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "4");


else if (e.getSource() == number_key.five)

if (com || i == 0)

result.text.setText("5");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "5");


else if (e.getSource() == number_key.six)

if (com || i == 0)

result.text.setText("6");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "6");


else if (e.getSource() == number_key.seven)

if (com || i == 0)

result.text.setText("7");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "7");


else if (e.getSource() == number_key.eight)

if (com || i == 0)

result.text.setText("8");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "8");



else if (e.getSource() == number_key.nine)


if (com || i == 0)

result.text.setText("9");
com = false;
i = 1;

else

sum = result.text.getText();
result.text.setText(sum + "9");




/*对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添
* 加0;否则保持原来的数值不变
*/
else if (e.getSource() == number_key.zero)

if (com || i == 0)

result.text.setText("0");
com = false;
i = 1;

else

sum = result.text.getText();
if (Float.parseFloat(sum) > 0 || Float.parseFloat(sum) < 0)

result.text.setText(sum + "0");

else

if (sum.trim().indexOf(".") == -1)

result.text.setText(sum);

else

result.text.setText(sum + "0");




else if (e.getSource() == number_key.ce)

result.text.setText("0");
i = 0;
com = true;

else if (e.getSource() == number_key.tzero)

if (com || i == 0)

result.text.setText("0");
com = false;
i = 1;

else

sum = result.text.getText();
if (Float.parseFloat(sum) > 0 || Float.parseFloat(sum) < 0)

result.text.setText(sum + "00");

else

if (sum.trim().indexOf(".") == -1)

result.text.setText(sum);

else

result.text.setText(sum + "00");




/*本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加,
* 不存在就添加.
*/
else if (e.getSource() == number_key.point)

if (com || i == 0)

result.text.setText("0.");
com = false;
i = 1;

else

sum = result.text.getText();
if (sum.trim().indexOf(".") == -1)

result.text.setText(sum + ".");

else

result.text.setText(sum);



//获得点击+之前的数值
else if (e.getSource() == number_key.plus)

com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
symbol = 1;

//获得点击-之前的数值
else if (e.getSource() == number_key.sub)

com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
symbol = 2;
//获得点击*之前的数值
else if (e.getSource() == number_key.mul)

com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
System.out.println(total);
symbol = 3;

//获得点击/之前的数值
else if (e.getSource() == number_key.div)

com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
symbol = 4;


else if (e.getSource() == number_key.equal)

switch (symbol)

case 1 ://计算加法

double ad =
total + Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
sum = "";
break;

case 2 ://计算减法

double ad =
total - Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
sum = "";
break;

case 3 ://计算乘法

double ad =
total * Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
sum = "";
break;

case 4 ://计算除法

double ad =
total / Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
sum = "";
break;


System.out.println(com);


public static void main(String[] args)

sakura ww = new sakura();
ww.setVisible(true) ;

参考技术A 晕,看看第二条。
不能运行的叫做程序吗?
参考技术B 靠 QQ:229300322是个女的啊 笨蛋 JAVA的没有啊 VC的倒是有啊 刚刚写好的啊

void CMfc_CounterDlg::OnAdd()

// TODO: Add your control notification handler code here

m_isBtnDown=TRUE; //判断+号是否按下
m_countType='+';

OnAmount() ; //计算结果


void CMfc_CounterDlg::OnMinus()

// TODO: Add your control notification handler code here
m_isBtnDown=TRUE; //判断-号是否按下
m_countType='-';

OnAmount() ; //计算结果


void CMfc_CounterDlg::OnChu()

// TODO: Add your control notification handler code here
m_isBtnDown=TRUE; //判断/号是否按下
m_countType='/';

OnAmount() ; //计算结果


void CMfc_CounterDlg::OnRide()

// TODO: Add your control notification handler code here
m_isBtnDown=TRUE; //判断*号是否按下
m_countType='*';

OnAmount() ; //计算结果


void CMfc_CounterDlg::OnEight()

OnCount('8'); //是按下那个数字


void CMfc_CounterDlg::OnFive()

OnCount('5'); //是按下那个数字


void CMfc_CounterDlg::OnFour()

OnCount('4'); //是按下那个数字


void CMfc_CounterDlg::OnNine()

OnCount('9'); //是按下那个数字


void CMfc_CounterDlg::OnOne()

// TODO: Add your control notification handler code here

OnCount('1'); //是按下那个数字


void CMfc_CounterDlg::OnSeven()

// TODO: Add your control notification handler code here
OnCount('7'); //是按下那个数字


void CMfc_CounterDlg::OnSix()

// TODO: Add your control notification handler code here
OnCount('6'); //是按下那个数字


void CMfc_CounterDlg::OnThree()

// TODO: Add your control notification handler code here
OnCount('3'); //是按下那个数字


void CMfc_CounterDlg::OnTwo()

// TODO: Add your control notification handler code here
OnCount('2'); //是按下那个数字


void CMfc_CounterDlg::OnZero()

// TODO: Add your control notification handler code here
OnCount('0'); //是按下那个数字


void CMfc_CounterDlg::OnAmount()

// TODO: Add your control notification handler code here

m_isBtnDown=TRUE; //判断=号是否按下

m_secondNum=atof(m_number);

SetDlgItemText(IDC_EDIT1,m_number);

if(m_countType=='+')

m_count.Format("%lf",m_firstNum+m_secondNum);

else if(m_countType=='-')

if(m_firstNum!=0)

m_count.Format("%lf",m_firstNum-m_secondNum);

else
m_firstNum=m_secondNum;
m_count.Format("%lf",m_firstNum);


else if(m_countType=='*')

m_count.Format("%lf",m_firstNum*m_secondNum);


else if(m_countType=='/')

if(m_secondNum!=0)

m_count.Format("%lf",m_firstNum/m_secondNum);

else

m_number="被除数不能为0,重新输入!";
UpdateData(FALSE);



UpdateData(FALSE);
SetDlgItemText(IDC_EDIT1,m_count);



void CMfc_CounterDlg::OnCount(char ch) //是按下那个数字


if(m_isBtnDown==TRUE ) //如果用户按下了 + - * / 其中一个

UpdateData();
m_firstNum=atof(m_number);
m_number.Empty();
m_number=m_number+ch;
SetDlgItemText(IDC_EDIT1,m_number);
UpdateData(FALSE);

m_isBtnDown=FALSE;


else

UpdateData();

m_number=m_number+ch;
m_secondNum=atof(m_number);
SetDlgItemText(IDC_EDIT1,m_number);

UpdateData(FALSE);




int CMfc_CounterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)

if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
m_secondNum =0;
m_firstNum =0;

m_menu.LoadMenu(IDR_MENU1);
SetMenu(&m_menu);
m_menu.Detach();

return 0;


void CMfc_CounterDlg::OnShowWindow(BOOL bShow, UINT nStatus)

CDialog::OnShowWindow(bShow, nStatus);

((CEdit*)GetDlgItem(IDC_EDIT1))->SetLimitText(12);

SetWindowText("模拟Windows计算器");
// TODO: Add your message handler code here



void CMfc_CounterDlg::OnPoint()

// TODO: Add your control notification handler code here

CString str;
GetDlgItemText(IDC_EDIT1,str);

m_isPoint=m_number.Find('.');
if( m_isPoint==-1 && (str.Find('.')==-1) )

m_number=m_number+'.';
UpdateData(FALSE);




void CMfc_CounterDlg::OnChu2()

// TODO: Add your control notification handler code here
m_number="";
UpdateData(FALSE);


void CMfc_CounterDlg::OnMenuitem32771()

// TODO: Add your command handler code here
InitializeSkin(_T("XPCorona.ssk"));
CloseWindow();
OpenIcon();


void CMfc_CounterDlg::OnMenuitem32772()

// TODO: Add your command handler code here
InitializeSkin(_T("SoftCrystal.ssk"));
CloseWindow();
OpenIcon();


void CMfc_CounterDlg::OnMenuitem32773()

// TODO: Add your command handler code here
InitializeSkin(_T("Minimized.ssk"));
CloseWindow();
OpenIcon();


void CMfc_CounterDlg::OnClose()

// TODO: Add your message handler code here and/or call default
ExitSkin();
CDialog::OnClose();
参考技术C 评论里放不下了。
是我以前写的,里面有许多废代码,可能对你帮助。注释我都加了,你自己生成JavaDOC看看。在NetBeans下编译运行通过。

全部代码请参见我的blog(n年没更新了)
http://tearcry.bokee.com/6024357.html
参考技术D import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter2 extends WindowAdapter

static JFrame f=new JFrame("¼ÆËãÆ÷");
static JTextField text1=new JTextField("0.");
static String source="";
static String cal="";
static String object="";
static boolean flag=false;
static boolean flag1=true;
static boolean flag2=false;
public void init()

try

Container c=f.getContentPane();
JPanel pan1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton bclar=new JButton("ÇåÁã");
text1.setHorizontalAlignment(JTextField.RIGHT);
c.add(text1,"North");
c.add(pan1);
A aa=new A();
Result re=new Result();
Opertion op=new Opertion();
Clar cl=new Clar();
b1.addActionListener(aa);
b2.addActionListener(aa);
b3.addActionListener(aa);
b4.addActionListener(aa);
b5.addActionListener(aa);
b6.addActionListener(aa);
b7.addActionListener(aa);
b8.addActionListener(aa);
b9.addActionListener(aa);
b0.addActionListener(aa);
b11.addActionListener(op);
b12.addActionListener(op);
b13.addActionListener(op);
b14.addActionListener(op);
b16.addActionListener(re);
b15.addActionListener(aa);
bclar.addActionListener(cl);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b11);
pan1.add(b4);
pan1.add(b5);
pan1.add(b6);
pan1.add(b12);
pan1.add(b7);
pan1.add(b8);
pan1.add(b9);
pan1.add(b13);
pan1.add(b0);
pan1.add(b15);
pan1.add(b16);
pan1.add(b14);
pan1.add(bclar);
f.setSize(200,220);
f.setVisible(true);

catch(Exception e)

System.out.println(e.getMessage());



class A implements ActionListener

public void actionPerformed(ActionEvent e)

String a=text1.getText();
String s=e.getActionCommand();
if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
text1.setText(s);
else
if(flag2)

text1.setText(s);
flag2=false;

else
text1.setText(a+s);




class Opertion implements ActionListener

public void actionPerformed(ActionEvent e)

cal=e.getActionCommand();
if(flag1==true)
source=text1.getText();
text1.setText(cal);
flag1=false;
flag=true;


class Result implements ActionListener

public void actionPerformed(ActionEvent e)

double num1;
num1=Double.parseDouble(source);
object=text1.getText();
double num2;
num2=Double.parseDouble(object);
double result=0;
if(cal.equals("+"))
result=num1+num2;
if(cal.equals("-"))
result=num1-num2;
if(cal.equals("*"))
result=num1*num2;
if(cal.equals("/"))
if(num2==0)
text1.setText("³ýÊý²»ÄÜΪ0");
else
result=num1/num2;
String s1=Double.toString(result);
text1.setText(s1);
flag1=true;
flag2=true;


class Clar implements ActionListener

public void actionPerformed(ActionEvent e)

text1.setText("0.");



public static void main(String[] args)

Counter2 count=new Counter2();
count.init();


public void windowClosing(WindowEvent e)
System.exit(1);

public void windowOpened(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
第5个回答  2007-01-10 我没学过JAVA delphi做过一个:
unit jisanqi;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

type
TForm1 = class(TForm)
Edit1: TEdit;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
Edit2: TEdit;
SpeedButton1: TSpeedButton;
Label1: TLabel;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
procedure SpeedButton1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Private declarations
public
Public declarations
end;

var
Form1: TForm1;

implementation

$R *.dfm

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
try
if (RadioButton1.Checked) then Edit3.Text:=FloatToStr(StrToFloat(Edit1.Text)+ StrToFloat(Edit2.Text)); if (RadioButton2.Checked) then Edit3.Text:=FloatToStr(StrToFloat(Edit1.Text)- StrToFloat(Edit2.Text)); if (RadioButton3.Checked) then Edit3.Text:=FloatToStr(StrToFloat(Edit1.Text)* StrToFloat(Edit2.Text)); if (RadioButton4.Checked) then Edit3.Text:=FloatToStr(StrToFloat(Edit1.Text)/ StrToFloat(Edit2.Text)); except ShowMessage('请检查输入数字的正确性.');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.clear;
Edit2.clear;
Edit3.clear;

end;

end.

HDU2188(巴什博奕)

悼念512汶川大地震遇难同胞——选拔志愿者
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5614    Accepted Submission(s): 3544


Problem Description
对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,差点儿每一个省市都派出了大量的救援人员,这当中包括抢险救灾的武警部队。治疗和防疫的医护人员,以及进行心理疏导的心理学专家。

依据要求,我校也有一个奔赴灾区救灾的名额。因为广大师生报名踊跃,学校不得不进行选拔来决定最后的人选。经过多轮的考核,形势逐渐明朗,最后的名额将在“林队”和“徐队”之间产生。可是非常巧合。2个人的简历差点儿一模一样,这让主持选拔的8600非常是为难。

无奈,他决定通过捐款来决定两人谁能入选。

选拔规则例如以下: 1、最初的捐款箱是空的; 2、两人轮流捐款,每次捐款额必须为正整数,而且每人每次捐款最多不超过m元(1<=m<=10)。 3、最先使得总捐款额达到或者超过n元(0<n<10000)的一方为胜者。则其能够亲赴灾区服务。

我们知道。两人都非常想入选志愿者名单,而且都是非常聪明的人,假设林队先捐,请你推断谁能入选最后的名单? Input 输入数据首先包括一个正整数C。表示包括C组測试用例,然后是C行数据,每行包括两个正整数n。m,n和m的含义參见上面提到的规则。

Output 对于每组測试数据,假设林队能入选。请输出字符串"Grass", 假设徐队能入选,请输出字符串"Rabbit",每一个实例的输出占一行。 Sample Input 2 8 10 11 10 Sample Output Grass Rabbit Author lcy


和hdu1846一样裸。



/***********************************************************
    > OS     : Linux 3.13.0-24-generic (Mint-17)
    > Author : yaolong
    > Mail   : [email protected]
    > Time   : 2014年10月25日 星期六 11时43分33秒
 **********************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while ( n-- )
    {
        int a, b;
        cin >> a >> b;
        if ( a % ( b + 1 ) == 0 )
        {
            cout << "Rabbit" << endl;
        }
        else
        {
            cout << "Grass" << endl;
        }
    }
    return 0;
}


以上是关于java做一个计算器,请各位伸出援助之手,小女子感激不尽的主要内容,如果未能解决你的问题,请参考以下文章

软路由如何设置?

HDU2188(巴什博奕)

python里的爬虫如何使用xpath 提取script里的元素?

西安东大肛肠医院黑心骗人医院收费很低,诋毁东大

游戏更新日志Hotfix补丁3

机会属于敢赌的人