java计算器代码要注释,java计算器代码要注释嘛
求JAVA编写的 简易计算器程序,附上注释
1、首先准备好软件即eclipse和java,下载安装完成后打开eclipse。
成都创新互联专注于文山州网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供文山州营销型网站建设,文山州网站制作、文山州网页设计、文山州网站官网定制、微信小程序开发服务,打造文山州网络公司原创品牌,更为您提供文山州网站排名全网营销落地服务。
2、点击左上角的file 新建一个project。
3、给project取一个名字,其他的选项都是默认然后点击finish。
4、接下来是新建一个class。
5、在给class取名字的时候注意用英文名的首字母要大写。完成后点击finish。
6、这就是开始写代码的工作台,将代码写在绿字下。
7、这就是第一个hello world程序。
java写的计算器代码,有注释
//计算器
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class M10 extends WindowAdapter implements MouseListener {
private String first;
private String second;
private String operator;
private Button zero = new Button("0");
private Button one = new Button("1");
private Button two = new Button("2");
private Button three = new Button("3");
private Button four = new Button("4");
private Button five = new Button("5");
private Button six = new Button("6");
private Button seven = new Button("7");
private Button eight = new Button("8");
private Button nine = new Button("9");
private Button decimal = new Button(".");
private Button equal = new Button("=");
private Button add = new Button("+");
private Button sub = new Button("-");
private Button mul = new Button("*");
private Button div = new Button("/");
private TextField input = new TextField();
private Button CE = new Button("CE");
private Button DEL = new Button("Del");
public static void main(String[] args){
new M10();
}
public M10(){
Frame f = new Frame("Calculator");
f.add("North", input);
Panel keys = new Panel();
f.add(keys, "Center");
keys.setLayout(new GridLayout(4, 4));
keys.add(seven);
keys.add(eight);
keys.add(nine);
keys.add(add);
keys.add(four);
keys.add(five);
keys.add(six);
keys.add(sub);
keys.add(one);
keys.add(two);
keys.add(three);
keys.add(mul);
keys.add(zero);
keys.add(decimal);
keys.add(equal);
keys.add(div);
zero.addMouseListener(this);
one.addMouseListener(this);
two.addMouseListener(this);
three.addMouseListener(this);
four.addMouseListener(this);
five.addMouseListener(this);
six.addMouseListener(this);
seven.addMouseListener(this);
eight.addMouseListener(this);
nine.addMouseListener(this);
add.addMouseListener(this);
sub.addMouseListener(this);
div.addMouseListener(this);
mul.addMouseListener(this);
equal.addMouseListener(this);
decimal.addMouseListener(this);
Panel addtionalPanel = new Panel();
addtionalPanel.setLayout(new GridLayout(1, 2));
addtionalPanel.add(CE);
addtionalPanel.add(DEL);
CE.setBackground(Color.green);
DEL.setBackground(Color.cyan);
CE.addMouseListener(this);
DEL.addMouseListener(this);
f.add("South", addtionalPanel);
f.addWindowListener(this);
f.setVisible(true);
f.setLocation(200, 300);
f.setSize(200, 200);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void mouseClicked(MouseEvent e){
Button btn = (Button)e.getSource();
String key = btn.getActionCommand().trim();
if(btn == one || btn == two || btn == three || btn == zero || btn == four || btn == five || btn == six || btn == seven || btn == eight || btn == nine){
if(isBlank(operator)){
if(isBlank(first)){
first = key;
}
else{
first += key;
}
input.setText(first);
}
else{
if(isBlank(second)){
second = key;
}
else{
second += key;
}
input.setText(second);
}
}
else if(btn == decimal){
if(isBlank(operator)){
if(isBlank(first)){
first = "0.";
input.setText(first);
}
else{
if(first.indexOf(".") == -1){
first += ".";
input.setText(first);
}
}
}
else{
if(isBlank(second)){
second = "0.";
input.setText(second);
}
else{
if(second.indexOf(".") == -1){
second += ".";
input.setText(second);
}
}
}
}
else if(btn == equal){
if(!isBlank(second) !isBlank(first) !isBlank(operator)){
double result = 0.0D;
if(operator.equals("+")){
result = Double.parseDouble(first) + Double.parseDouble(second);
}
else if(operator.equals("-")){
result = Double.parseDouble(first) - Double.parseDouble(second);
}
else if(operator.equals("*")){
result = Double.parseDouble(first) * Double.parseDouble(second);
}
else if(operator.equals("/")){
result = Double.parseDouble(first) / Double.parseDouble(second);
}
int value = (int)Math.round(result);
if(value == result){
input.setText(String.valueOf(value));
}
else{
input.setText(String.valueOf(result));
}
first = String.valueOf(result);
second = null;
operator = null;
}
}
else if(btn == add || btn == sub || btn == div || btn == mul){
if(!isBlank(first)){
if(!isBlank(operator) !isBlank(second)){
if(operator.equals("+")){
first = String.valueOf(Double.parseDouble(first) + Double.parseDouble(second));
}
else if(operator.equals("-")){
first = String.valueOf(Double.parseDouble(first) - Double.parseDouble(second));
}
else if(operator.equals("*")){
first = String.valueOf(Double.parseDouble(first) * Double.parseDouble(second));
}
else if(operator.equals("/")){
first = String.valueOf(Double.parseDouble(first) / Double.parseDouble(second));
}
second = null;
}
operator = key;
}
}
else if(btn == CE){
first = null;
second = null;
operator = null;
input.setText(null);
}
else if(btn == DEL){
if(isBlank(operator)){
if(!isBlank(first)){
first = first.substring(0, first.length() -1);
input.setText(first);
}
}
else{
if(!isBlank(second)){
second = second.substring(0, second.length() -1);
input.setText(second);
}
}
}
}
private boolean isBlank(String str){
return str == null || str.trim().equals("");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
java 计算器代码带括号 求注释
//引入各种类文件
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//定义JsqFrame继承Frame
class JsqFrame extends Frame {
double d1, d2; //定义两个double类型的变量
int op = -1; //定义两个int类型的变量
static TextField tf;//定义静态对象TextField
CalPanelL p1; //定义CalPanelL对象
// Constructor构造方法
JsqFrame() {
//以下设置属性
super("计算器");
setLayout(new FlowLayout());
setResizable(false);
setSize(250, 250);
tf = new TextField(18);
tf.setEditable(false);
tf.setBackground(Color.lightGray);
tf.setForeground(Color.red);
tf.setFont(new Font("Arial", Font.BOLD, 16));
add(tf);
p1 = new CalPanelL();
add(p1);
setVisible(true);
// addWindowListener(new Wclose());
}
//添加按钮继承Button类
class CalButton extends Button {
CalButton(String s) {
//设置按钮属性
super(s);
setBackground(Color.WHITE); //设置颜色为白色
}
}
//定义显示器继承Panel类
class CalPanelL extends Panel {
CalButton a, c, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bPN, bPoint,
bAdd, bSub, bMul, bDiv, bL, bR, bLn, bEqual, bCE, bQuit;
CalPanelL() {
//设置显示器的属性
setLayout(new GridLayout(6, 4));
setFont(new Font("TimesRoman", Font.BOLD, 16));
a = new CalButton("");
c = new CalButton("");
b0 = new CalButton("0");
b1 = new CalButton("1");
b2 = new CalButton("2");
b3 = new CalButton("3");
b4 = new CalButton("4");
b5 = new CalButton("5");
b6 = new CalButton("6");
b7 = new CalButton("7");
b8 = new CalButton("8");
b9 = new CalButton("9");
bPN = new CalButton("+/-");
bPoint = new CalButton(".");
// 设置按钮
bAdd = new CalButton("+");
bSub = new CalButton("-");
bMul = new CalButton("*");
bDiv = new CalButton("/");
bL = new CalButton("(");
bR = new CalButton(")");
bLn = new CalButton("ln");
bEqual = new CalButton("=");
bCE = new CalButton("CE");
bQuit = new CalButton("退出");
// 加入按钮
add(a);
add(c);
add(bCE);
bCE.addActionListener(new PressBCE());
add(bQuit);
bQuit.addActionListener(new PressBQuit());
add(b7);
b7.addActionListener(new PressB7());
add(b8);
b8.addActionListener(new PressB8());
add(b9);
b9.addActionListener(new PressB9());
add(bDiv);
bDiv.addActionListener(new PressBDiv());
add(b4);
b4.addActionListener(new PressB4());
add(b5);
b5.addActionListener(new PressB5());
add(b6);
b6.addActionListener(new PressB6());
add(bMul);
bMul.addActionListener(new PressBMul());
add(b1);
b1.addActionListener(new PressB1());
add(b2);
b2.addActionListener(new PressB2());
add(b3);
b3.addActionListener(new PressB3());
add(bSub);
bSub.addActionListener(new PressBSub());
add(b0);
b0.addActionListener(new PressB0());
add(bPoint);
bPoint.addActionListener(new PressBPoint());
add(bPN);
bPN.addActionListener(new PressBPN());
;
add(bAdd);
bAdd.addActionListener(new PressBAdd());
add(bL);
bL.addActionListener(new PressBL());
add(bR);
bR.addActionListener(new PressBR());
add(bLn);
bLn.addActionListener(new PressBLn());
add(bEqual);
bEqual.addActionListener(new PressBEqual());
}
}
//定义PressBAdd实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[加号的监听事件]
class PressBAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 0;
tf.setText(d1 + "+");
} catch (Exception ee) {
}
}
}
//定义PressBSub实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[减号的监听事件]
class PressBSub implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 1;
tf.setText(d1 + "-");
} catch (Exception ee) {
}
}
}
//定义PressBMul实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[乘号的监听事件]
class PressBMul implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 2;
tf.setText(d1 + "*");
} catch (Exception ee) {
}
}
}
//定义PressBDiv实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[除号的监听事件]
class PressBDiv implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 3;
tf.setText(d1 + "/");
} catch (Exception ee) {
}
}
}
//定义PressBL实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[向左键的监听事件]
class PressBL implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 3;
tf.setText(d1 + "(");
} catch (Exception ee) {
}
}
}
//定义PressBR实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[向右键的监听事件]
class PressBR implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 3;
tf.setText(d1 + ")");
} catch (Exception ee) {
}
}
}
//定义PressBEqual实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[等号的监听事件]
class PressBEqual implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
Jsq jsq = new Jsq();
String s = tf.getText();
System.out.println(s);
while (s.indexOf("(") + 1 0 s.indexOf(")") 0) {
String s1 = jsq.caculateHigh(s);
System.out.println(s1);
s = s1;
}
String str = jsq.cacluteMain(s);
System.out.println(str);
tf.setText(String.valueOf(str));
} catch (Exception ee) {
}
}
}
/*
* 批量写吧
* 以下是按1、2、3等等的监听事件
* 还有清零的等等监听事件
*/
class PressBLn implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.log10(x);
tf.setText(y + "");
} catch (Exception ee) {
}
}
}
class PressBCE implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf.setText("");
}
}
class PressBPN implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String text = tf.getText();
if (text != "") {
if (text.charAt(0) == '-')
tf.setText(text.substring(1));
else if (text.charAt(0) = '0' text.charAt(0) = '9')
tf.setText("-" + text.substring(0));
else if (text.charAt(0) == '.')
tf.setText("-0" + text.substring(0));
}
} catch (Exception ee) {
}
}
}
class PressBPoint implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
if (text.lastIndexOf(".") == -1)
tf.setText(text + ".");
}
}
class PressB0 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "0");
}
}
class PressB1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "1");
}
}
class PressB2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "2");
}
}
class PressB3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "3");
}
}
class PressB4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "4");
}
}
class PressB5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "5");
}
}
class PressB6 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "6");
}
}
class PressB7 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "7");
}
}
class PressB8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "8");
}
}
class PressB9 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "9");
}
}
class PressBQuit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public void Js() {
String text = tf.getText();
tf.setText(text);
}
}
当前标题:java计算器代码要注释,java计算器代码要注释嘛
标题路径:http://scjbc.cn/article/dsiiohj.html