用JAVA编一个计算器的程序,要里边的注释
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA编一个计算器的程序,要里边的注释相关的知识,希望对你有一定的参考价值。
计算器
项目概述
计算器
项目名称 计算器
代码量 600 行 项目简介 通过Java控制台程序完成Windows中附件中运算器的功能。
项目目的 通过一个Java控制台程序,流程控制语句的熟练运用程度,对于Java异常处理的掌握和运用程度,以及编码规范度。
通过对项目进行重构,深入领悟OOP技术。
涉及主要技术 Java基本语法
Java输入输出
Java异常控制
Java输入输出
Java容器类
数据库 无
编程环境 JDK 1.5
JCreator / NetBeans
项目特点 控制台界面,单机版简单程序。
技术重点 Java流程控制语句的熟练运用
Java异常处理
从面向过程思想到面向对象思想的转变
技术难点 Java I/O使用(从控制台接收和输出信息)
Java 容器类基本使用
OOP的具体实现
图形界面:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Counter implements ActionListener
JFrame counter = null;
Container cou = null;
JTextField txt = null;
JPanel pan = null;
JButton [] btn = null;
JLabel lblbrand = null,lbltype = null;
String sign = "",sign1 = "0",sign2 = "",sign3 = "";
char operator = ' ', operator1 = '+';
int i = 0;
double resault = 0;
int b = 0;
public Counter()
counter = new JFrame("计算器");
cou = counter.getContentPane();
cou.setLayout(null);
lblbrand = new JLabel("大象牌");
lblbrand.setForeground(Color.BLUE);
lblbrand.setBounds(15,8,70,20);
cou.add(lblbrand);
lbltype = new JLabel("x-14250");
lbltype.setForeground(Color.GREEN);
lbltype.setBounds(265,8,70,20);
cou.add(lbltype);
txt = new JTextField("0.");
txt.setCaretPosition(2);
txt.setHorizontalAlignment(JTextField.TRAILING);
cou.add(txt);
txt.setBounds(15,28,300,30);
pan = new JPanel(new GridLayout(5,4,10,10));
pan.setBounds(15,68,300,160);
btn = new JButton[20];
String [] btnname = "Off","Back","C","Exit","7","8","9","+","4","5",
"6","-","1","2","3","*","0",".","=","/";
for(int i = 0; i < 20; i++)
btn[i] = new JButton(btnname[i]);
pan.add(btn[i]);
btn[i].addActionListener(this);
btn[7].setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
btn[11].setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
btn[15].setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
btn[17].setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
btn[18].setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
btn[19].setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
cou.add(pan);
counter.setSize(335,280);
counter.setVisible(true);
public void actionPerformed(ActionEvent e)
operator = e.getActionCommand().charAt(0);
annal();
if(!sign.equals(""))
opinion(sign);
else
showresault(sign1);
if(i % 2 != 0)
txt.setText("");
private void annal()
int l = 0;
switch (operator)
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!sign.equals("0"))
sign = sign + Character.toString(operator);
else
sign = sign.concat(Character.toString(operator)).substring(1);
break;
case '0':
if (!sign.equals("") && !sign.equals("0"))
sign = sign + Character.toString(operator);
else
sign = "0";
break;
case 'B':
if (sign.length() > 1 && sign.charAt(txt.getText().length() - 1) != '.')
sign = (sign.substring(0,sign.length() - 1));
else if (sign.length() > 2)
sign = (sign.substring(0,sign.length() - 2));
else
sign = "0";
break;
case 'C':
sign = "";
sign2 = "";
sign1 = "0";
operator = ' ';
break;
case 'E':
System.exit(0);
break;
case 'O':
sign = "";
sign2 = "";
sign1 = "0";
operator = ' ';
i++;
break;
case '+':
case '-':
case '*':
case '/':
count();
break;
case '=':
count();
operator1 = ' ';
sign3 = "-1";
break;
case '.':
if (sign.equals(""))
sign = "0".concat(Character.toString(operator));
else
for (int i = 0; i < sign.length(); i++)
if (sign.charAt(i) == '.')
l++;
if (l == 0)
sign = sign + Character.toString(operator);
break;
private void count()
double number1 = 0,number2 = 0;
if (!sign.equals(""))
sign2 = sign;
else
sign2 = "0";
sign = "";
if (!sign3.equals("-1"))
sign3 = sign1;
else
sign3 = "0";
number1 = Double.valueOf(sign3).doubleValue();
number2 = Double.valueOf(sign2).doubleValue();
switch(operator1)
case '+':
resault = number1 + number2;
break;
case '-':
resault = number1 - number2;
break;
case '*':
resault = number1 * number2;
break;
case '/':
resault = number1 / number2;
break;
case ' ':
resault = Double.valueOf(sign2).doubleValue();
operator1 = operator;
sign1 = Double.toString(resault);
private void showresault(String sign)
int j = sign.length() - 1;
String subsign = "";
for (int i = 0; i <= j; i++)
if (sign.charAt(i) == '.')
j = i;
break;
if (j != sign.length() - 1)
subsign = sign.substring(j+1);
if (Double.valueOf(subsign).doubleValue() > 0)
txt.setText(sign);
else
txt.setText(sign.substring(0, j));
else
txt.setText(sign);
private void opinion(String sign)
txt.setText(sign);
public static void main(String[] args)
Counter counter = new Counter();
这个是以前自己做的一个计算器,可能不是很完善,注释没写,但代码相对简单,应该可以看明白本回答被提问者采纳
摩斯码编解码器
1、背景
今天是1024,程序员节那就干点儿程序员的事情。刚好,记得上高中时候,看过一部电影,无间道,里边黄秋生和梁朝伟用摩斯码通信,瞬间觉得好神秘,好帅气。最近闲来无事,出于对当初兴趣的尊敬,就顺手实现了一款简易的摩斯编解码器。
2、编解码设计
自己玩儿,暂定中文摩斯编码。基本思想是,将汉字对应的四位区位码数字分别编码为数字对应的摩斯码,一个中文对应20位摩斯码。解码过程逆向。
3、代码实现
首先,项目结构图如下:
因为是个小工具,整个项目采用WPF实现。
主界面:
就核心过程而言,编码分两步:
1、汉字转区位码
此算法是直接抄的,原始出处,有点儿懒得费功夫找了,再次声明,不是在下原创。
/// <summary> /// 汉字转区位码方法 /// </summary> /// <param name="chinese">汉字</param> /// <returns>区位码</returns> public static string ChineseToCoding(string chinese) { string pCode = ""; byte[] pArray = new byte[2]; pArray = Encoding.GetEncoding("GB2312").GetBytes(chinese);//得到汉字的字节数组 int front = (short)(pArray[0] - ‘