求一个简单的JAVA计算器代码

a, b=0.008,sum, n
if (n<=6)
a=6;
if(n%6=0)
a=n%6;
if(n%6!=0)
a=int n%6+1
sum=a*b
sum=sum+1
这是我大体的思路
输入数据n当满足以上条件的时候求和
这是一个计算程序思路,输入数值,当满足以上条件时候,把所得的值相加,求和

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter 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)
{
Counter count=new Counter();
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){}
}

参考资料:http://zhidao.baidu.com/question/2471672.html

温馨提示:内容为网友见解,仅供参考
第1个回答  2007-04-25
你这个需求思路不清晰啊,别人不知道怎么下手
第2个回答  2007-04-26
我想问问你这个程序是干吗的~?
思路太清晰了~
就是不知道有什么意义~?

java中写了一个简单计算器,计算0.3*3=0.8999...,有人说要用BigDecimal...
代码如下:double d = 0.3;BigDecimal bd = new BigDecimal(d+"");BigDecimal bd2 = new BigDecimal(3);System.out.println(bd.multiply(bd2));--- 需要注意的是, BigDecimal bd = new BigDecimal(d+"");构造函数要传String类型,如果传double类型的话,结果也是0.8999......

用java编写科学计算器的代码!一定要有求立方
import java.awt.event.*;class Counter 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 boolea...

...于真实计算器类似,功能简单,最好能有按键声音的java计算器代码...
p6.setLayout(new GridLayout(4,5,3,3));String[] KEYS = { "7", "8", "9", "\/", "sqrt", "4", "5", "6","*", "%", "1", "2", "3", "-", "1\/x", "0", "+\/-", ".", "+", "=" };Button []keys=new Button[KEYS.length];for (int i = 0; i ...

求JAVA计算器核心源代码?
按钮b1,数字1的按钮JButton b1=new JButton("1");文本框JTextField jtp=new JTextField();b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jtp.setText("1");} });b.addActionListener(this);\/\/按钮b添加事件监听器 addWindowListener(new WindowAdapter(...

Java程序--编写一个具有图形界面的计算器程序
import java.awt.*;import java.awt.event.*;import java.io.*;public class Test implements ActionListener { Frame f = new Frame("一步运算计算器");\/\/创建窗架 TextField[] tf = new TextField[3];Label La1 = new Label("+");Button btn = new Button("=");int[] a = new ...

java计算器代码,只有加减乘除和取余运算的??
import java.awt.*;import java.awt.event.*;import java.lang.*;import javax.swing.*;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,...

制作java计算器时怎样让它实现开平方功能?代码是什么
java设计计算器,主要是使用java swing,对每个按钮进行监听,实现相关功能,如下代码: package com.qiu.lin.he; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import ja...

用java写一个命令行计算器
import java.util.Scanner;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class Kyo{public static void main(String[] args){ScriptEngineManager sem = new ScriptEngineManager();ScriptEngine se = sem.getEngineByName("js");Scanner...

计算器java代码(基本的加减乘除) 能运行 的程序 直接 发到我的邮箱 谢...
super("计算器");setSize(300,240);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Container con=getContentPane();con.setLayout(new BorderLayout());jPanel1=new JPanel();jPanel1.setLayout(new GridLayout(1,1));jPanel2=new JPanel();jPanel2.setLayout(new GridLayout...

求一个JAVA程序
import java.awt.*;import java.applet.*;\/\/引入所需的类包import java.awt.*;import java.applet.*;\/\/定义类Calculator.javapublic class Calculator extends Applet{ private TextField tfAnswer;\/\/计算器的显示文本框 private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;\/\/数字0~9的按钮 \/\/定义小数点,...

相似回答
大家正在搜