重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java连点器代码 连点器的代码

java 写的计算器源代码只实现加减乘除四则运算即可

import java.awt.*;

创新互联专注于乐业企业网站建设,响应式网站建设,商城开发。乐业网站建设公司,为乐业等地区提供建站服务。全流程按需求定制制作,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

import java.awt.event.*;

import javax.swing.*;

import java.util.Vector;

public class calculator

{

String str1="0"; //运算数1 初值一定为0 为了程序的安全

String str2="0"; //运算数2

String fh="+"; //运算符

String jg="";//结果

//状态开关 重要

int k1=1;//开关1 用于选择输入方向 将要写入str2或 str2

int k2=1;//开关2 符号键 次数 k21说明进行的是2+3-9+8 这样的多符号运算

int k3=1;//开关3 str1 是否可以被清0 ==1时可以 !=1时不能被清0

int k4=1;//开关4 str2 同上

int k5=1;//开关5 控制小数点可否被录入 ==1时可以 !=1 输入的小数点被丢掉

JButton jicunqi; //寄存器 记录 是否连续按下符号键

Vector vt=new Vector(20,10);

JFrame frame=new JFrame("sunshine---计算器");

JTextField jg_TextField=new JTextField(jg,20);//20列

JButton clear_Button=new JButton("清除");

JButton button0=new JButton("0");

JButton button1=new JButton("1");

JButton button2=new JButton("2");

JButton button3=new JButton("3");

JButton button4=new JButton("4");

JButton button5=new JButton("5");

JButton button6=new JButton("6");

JButton button7=new JButton("7");

JButton button8=new JButton("8");

JButton button9=new JButton("9");

JButton button_Dian=new JButton(".");

JButton button_jia=new JButton("+");

JButton button_jian=new JButton("-");

JButton button_cheng=new JButton("*");

JButton button_chu=new JButton("/");

JButton button_dy=new JButton("=");

public static void main(String[] args)

{

calculator calculator=new calculator();

}

calculator()

{

jg_TextField.setHorizontalAlignment(JTextField.RIGHT );//文本框 右对齐

JPanel pan=new JPanel();

pan.setLayout(new GridLayout(4,4,5,5));//四行四列 边距为5像素

pan.add(button7);

pan.add(button8);

pan.add(button9);

pan.add(button_chu);

pan.add(button4);

pan.add(button5);

pan.add(button6);

pan.add(button_cheng);

pan.add(button1);

pan.add(button2);

pan.add(button3);

pan.add(button_jian);

pan.add(button0);

pan.add(button_Dian);

pan.add(button_dy);

pan.add(button_jia);

pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));//pan对象的边距

JPanel pan2=new JPanel();

pan2.add(jg_TextField);

JPanel pan3=new JPanel(); //为什么要 多此一句呢? 因为我不会设置 按钮的大小

pan3.setLayout(new FlowLayout());

pan3.add(clear_Button);

//clear_Button.setSize(10,10);//设置清零按钮的大小 吗的 不好使 !!

frame.setLocation(300, 200); //主窗口 出现在位置

frame.setResizable(false); //不能调大小

frame.getContentPane().setLayout(new BorderLayout());

frame.getContentPane().add(pan2,BorderLayout.NORTH);

frame.getContentPane().add(pan,BorderLayout.CENTER);

frame.getContentPane().add(pan3,BorderLayout.SOUTH);

frame.pack();

frame.setVisible(true);

//以上是 控件 和 布局

//下面是事件处理 程 序

//--------------- 数 字 键 ----------------

class JianTing implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String ss=((JButton)e.getSource()).getText();

jicunqi=(JButton)e.getSource();

vt.add(jicunqi);

if (k1==1)

{

if(k3==1)

{

str1="";

k5=1;//还原开关k5状态

}

str1=str1+ss;

//k2=1;

k3=k3+1;

//System.out.println(str1);

jg_TextField.setText(str1);//显示

}

else if(k1==2)

{

if (k4==1)

{

str2="";

k5=1; //还原开关k5状态

}

str2=str2+ss;

//k2=2;

k4=k4+1;

///////////////测试////////////////

jg_TextField.setText(str2);

}

}

}

//--------符 号-----------

class JianTing_fh implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String ss2=((JButton)e.getSource()).getText();

jicunqi=(JButton)e.getSource();

vt.add(jicunqi);

if(k2==1)

{

k1=2;//开关 k1 为1时,向数1写 为2时,向数2写

k5=1;

fh=ss2;

k2=k2+1;//按符号键的次数

}

else

{

int a=vt.size();

JButton c=(JButton)vt.get(a-2); if(!(c.getText().equals("+"))!(c.getText().equals("-"))!(c.getText().equals("*"))!(c.getText().equals("/")))

{

yuns();

str1=jg;

k1=2;//开关 k1 为1时,向数1写 为2时,向数2写

k5=1;

k4=1;

fh=ss2;

} k2=k2+1;

}

}

}

//--------清除-------

class JianTing_clear implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jicunqi=(JButton)e.getSource();

vt.add(jicunqi);

k5=1;

k2=1;

k1=1;

k3=1;

k4=1;

str1="0";

str2="0";

fh="";

jg="";

jg_TextField.setText(jg);

vt.clear();

}

}

//----------------等 于 ---------------------

class JianTing_dy implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jicunqi=(JButton)e.getSource();

vt.add(jicunqi);

yuns();

k1=1; //还原开关k1状态

//str1=jg;

k2=1;

k3=1;//还原开关k3状态

k4=1; //还原开关k4状态

str1=jg; //为7+5=12 +5=17 这种计算做准备

}

}

//----------------小数点 ---------------------

class JianTing_xiaos implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jicunqi=(JButton)e.getSource();

vt.add(jicunqi);

if(k5==1)

{

String ss2=((JButton)e.getSource()).getText();

if (k1==1)

{

if(k3==1)

{

str1="";

k5=1; //还原开关k5状态

}

str1=str1+ss2;

//k2=1;

k3=k3+1;

//System.out.println(str1);

jg_TextField.setText(str1);//显示

}

else if(k1==2)

{

if (k4==1)

{

str2="";

k5=1; //还原开关k5状态

}

str2=str2+ss2;

//k2=2;

k4=k4+1;

///////////////测试////////////////

jg_TextField.setText(str2);

}

}

k5=k5+1;

}

}

//注册 监听器

JianTing_dy jt_dy=new JianTing_dy();

JianTing jt= new JianTing();//临听数字键

JianTing_fh jt_fh= new JianTing_fh();//临 听符 号键

JianTing_clear jt_c=new JianTing_clear(); //清除键

JianTing_xiaos jt_xs=new JianTing_xiaos();// 小数点 键

button7.addActionListener(jt);

button8.addActionListener(jt);

button9.addActionListener(jt);

button_chu.addActionListener(jt_fh);

button4.addActionListener(jt);

button5.addActionListener(jt);

button6.addActionListener(jt);

button_cheng.addActionListener(jt_fh);

button1.addActionListener(jt);

button2.addActionListener(jt);

button3.addActionListener(jt);

button_jian.addActionListener(jt_fh);

button0.addActionListener(jt);

button_Dian.addActionListener(jt_xs);

button_dy.addActionListener(jt_dy);

button_jia.addActionListener(jt_fh);

clear_Button.addActionListener(jt_c);

//关闭事件处理程序

frame.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

//---------------计 算------------------

public void yuns()

{

double a2,b2;//运算数1,2

String c=fh;// 运算符

double jg2=0 ;//结果

if (c.equals(""))

{

//System.out.println("请输入运算符");

jg_TextField.setText("请输入运算符");

}

else

{

System.out.println("str1:"+str1);//调试时 使 用

System.out.println("str2:"+str2);//调试时 使 用

System.out.println("运算符:"+fh);//调试时 使 用

if (str1.equals(".")) //字符串 "." 转换成double型数据时 会出错 所以手工转

str1="0.0";

if (str2.equals("."))

str2="0.0";

a2=Double.valueOf(str1).doubleValue();

b2=Double.valueOf(str2).doubleValue();

System.out.println("double型的a2:"+a2); //调试时 使 用

System.out.println("double型的b2:"+b2); //调试时 使 用

if (c.equals("+"))

{

jg2=a2+b2;

}

if (c.equals("-"))

{

jg2=a2-b2;

}

if (c.equals("*"))

{

jg2=a2*b2;

}

if (c.equals("/"))

{

if(b2==0)

{

jg2=0;//0000000000000 by 0 cu!

}

else

{

jg2=a2/b2;

}

}

System.out.println("double型a2"+fh+"b2结果:"+jg2);

System.out.println();

jg=((new Double(jg2)).toString());

jg_TextField.setText(jg);

}

}

}

怎么用java代码连接到服务器?

用Socket类去连接

String ip = "192.168.0.57";

int port=7000;

InputStream in;

OutputStream out;

Socket sock = null;

try {

sock = new Socket(ip,port);

sock.setSoTimeout(60*1000);//设置超时

this.in = sock.getInputStream();

this.out = sock.getOutputStream();

} catch (Exception e) {

throw new Exception("与终端连接失败!");

}

求JAVA做的计算器完整代码,Applet实现。

下面两个可以么,是我做实验答辩时用到的!

import java.awt.*;//AWT核心包

import java.awt.event.*;//提供事件类和监听器

public class Counter extends Frame implements ActionListener

{

TextField t=new TextField("");//文本框

Panel p1=new Panel();//new一个panel,用于存放数字键和符号键。

Panel p2=new Panel();//new一个panel,用于存放开方、平方、和清除键。

Button[] b=new Button[10];//实例化Button对象

Button bAdd=new Button("加");

Button bSub=new Button("减");

Button bMul=new Button("乘以");

Button bPoint=new Button(".");

Button bDiv=new Button("除以");

Button bEqual=new Button("等於");

Button bSqrt=new Button("开方");

Button bPow=new Button("平方");

Button bNull=new Button("清除");

String str1="";     //str1和str2存放两个输入的数

String str2="";

String operator=null;   //存放加减乘除以及开平方的符号

boolean first=true;    //检验输入的是否为第一个数

int countOper=0;    //累计输入符号的个数,连加连减等操作中会用到

double result=0.0;    //暂存结果

double num1=0.0,num2=0.0;  //两个输入的数做运算时转化为double存放

boolean error=false;   //检验除数是否为0

//构造方法

public Counter()

{

Frame s=new Frame("计算器");//创建Frame

for(int i=0;i10;i++)//利用for循环将数字键添加进p1中

{

b[i]=new Button(String.valueOf(i));

p1.add(b[i]);

b[i].setActionCommand("number");

b[i].setForeground(new Color(150,20,20));

b[i].addActionListener(this);//调用addActionListener()方法注册事件监听器

}

p1.add(bPoint);

bPoint.setActionCommand("number");

p1.add(bAdd); //数字键,符号键放置在panel的p1中

p1.add(bSub);

p1.add(bMul);

p1.add(bDiv);

p1.add(bEqual);

p2.add(bSqrt);//开方键,平方键,清除键放置在panel的p2中

p2.add(bPow);

p2.add(bNull);

bAdd.setActionCommand("oper");

bSub.setActionCommand("oper");

bMul.setActionCommand("oper");

bDiv.setActionCommand("oper");

bAdd.setForeground(Color.red);//为组键设计颜色

bSub.setForeground(Color.red);

bMul.setForeground(Color.red);

bDiv.setForeground(Color.red);

bPoint.setForeground(Color.black);

bEqual.setForeground(Color.red);

bSqrt.setForeground(Color.blue);

bPow.setForeground(Color.blue);

bNull.setForeground(Color.blue);

bAdd.addActionListener(this);//调用addActionListener()方法注册事件监听器

bSub.addActionListener(this);

bMul.addActionListener(this);

bDiv.addActionListener(this);

bPoint.addActionListener(this);

bEqual.addActionListener(this);

bSqrt.addActionListener(this);

bPow.addActionListener(this);

bNull.addActionListener(this);

p1.setLayout(new GridLayout(4,4,5,5));//网格布局管理器,把容器根据行数和列数分成同样大小的单元,

//每个单元可容纳一个组件,并且此组件会填满网格单元,不能控

//制其占据网格的大小。4、4为网格的行、列数。5、5为组建之间的

//间距

p2.setLayout(new FlowLayout());//用FlowLayout布局管理器将组建默认剧中排放,默认间隙为5个像素

add(t,"North");      //frame的north放置输入框,panel放置在center和south

add(p1,"Center");//将p1添加到Center中

add(p2,"South");//将p2添加到South中

setLocation(400,200);//设计按钮尺寸

setSize(200,200);//设计窗口尺寸

setBackground(new Color(20,200,10));//设置Frame的背景,默认为白色

setVisible(true);//设置Frame设置为可见

addWindowListener(new WindowAdapter(){   //关闭窗口功能

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

//实现接口ActionListener

public void actionPerformed(ActionEvent e)

{

Button temp=(Button)e.getSource();

if(e.getActionCommand().equals("number"))

{

if(first)

{

str1=str1+temp.getLabel();

t.setText(str1);//将输入的str1显示在文本框中

}

else

{

str2=str2+temp.getLabel();

t.setText(str2);//将输入的str2显示在文本框中

}

}

else if(e.getActionCommand().equals("oper"))

{

if(str1=="")    //如果还没有输入数就点击运算符执行if

{

countOper=0;//若此,则将计数清零

first=true;

}

else

{

countOper++;//计算输入符号的个数

if(countOper1)//若输入的符号个数多余一个,则可以进行计算

{

getResult();

}

operator=temp.getLabel();//存放加减乘除以及开方、平方的符号

first=false;

}

}

else if(e.getActionCommand().equals("开方"))

{

double d=Math.sqrt(Double.parseDouble(str1));

str1=String.valueOf(d);//将计算出来的结果再次传给str1,为连计算准备

t.setText(String.valueOf(d));//将计算出来的结果传至文本框中

first=false;//置为false,即已输入第一个数

}

else if(e.getActionCommand().equals("平方"))

{

double f=Math.pow(Double.parseDouble(str1),2);

str1=String.valueOf(f);

t.setText(String.valueOf(f));

first=false;

}

else if(e.getActionCommand().equals("清除"))

{

str1="";//清空

str2="";

t.setText("");//将文本框清空

countOper=0;//将按键计数器清零

first=true;

error=false;

}

else if(e.getActionCommand().equals("等於"))

{

if((str1=="")||(str2==""))   //两个数没有输全就点击等号,执行if

{

countOper=0;//将按键计数器清零

first=true;

}

else

{

getResult();

countOper=0;

}

}

}

//运算结果的方法

public void getResult()

{

num1=Double.parseDouble(str1);

num2=Double.parseDouble(str2);

if(operator.equals("加"))

{

result=num1+num2;

}

else if(operator.equals("减"))

{

result=num1-num2;

}

else if(operator.equals("乘以"))

{

result=num1*num2;

}

else if(operator.equals("除以"))

{

if(num2==0.0)    //除数为0的处理方法

{

error=true;

}

else

{

result=num1/num2;

}

}

if(error)

{

t.setText("error");

}

else

{

t.setText(String.valueOf(result));

str1=String.valueOf(result);  //运算后把结果放入str1中,str2清空,为连加连减等操作做准备

str2="";

}

}

//主方法

public static void main(String[] args)

{

new Counter();//创建一个对象"计算器"

}

}

import   java.awt.*;

import   java.awt.event.*;

import   javax.swing.*;

class   CalculatorPanel   extends   JPanel

implements   ActionListener

{     public   CalculatorPanel()

{     setLayout(new   BorderLayout());

display   =   new   JTextField("0");

display.setEditable(false);

add(display,   "North");

JPanel   p   =   new   JPanel();

p.setLayout(new   GridLayout(4,   4));

String   buttons   =   "789/456*123-0.=+";

for   (int   i   =   0;   i      buttons.length();   i++)

addButton(p,   buttons.substring(i,   i   +   1));

add(p,   "Center");

}

private   void   addButton(Container   c,   String   s)

{     JButton   b   =   new   JButton(s);

c.add(b);

b.addActionListener(this);

}

public   void   actionPerformed(ActionEvent   evt)

{     String   s   =   evt.getActionCommand();

if   ('0'   =   s.charAt(0)      s.charAt(0)   =   '9'

||   s.equals("."))

{     if   (start)   display.setText(s);

else   display.setText(display.getText()   +   s);

start   =   false;

}

else

{     if   (start)

{     if   (s.equals("-"))

{   display.setText(s);   start   =   false;   }

else   op   =   s;

}

else

{     calculate(Double.parseDouble(display.getText()));

op   =   s;

start   =   true;

}

}

}

public   void   calculate(double   n)

{     if   (op.equals("+"))   arg   +=   n;

else   if   (op.equals("-"))   arg   -=   n;

else   if   (op.equals("*"))   arg   *=   n;

else   if   (op.equals("/"))   arg   /=   n;

else   if   (op.equals("="))   arg   =   n;

display.setText(""   +   arg);

}

private   JTextField   display;

private   double   arg   =   0;

private   String   op   =   "=";

private   boolean   start   =   true;

}

public   class   CalculatorApplet   extends   JApplet

{     public   void   init()

{     Container   contentPane   =   getContentPane();

contentPane.add(new   CalculatorPanel());

}

}

java编一个计算器的代码

界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!

代码如下:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Counter {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();

}

}

class CounterFrame extends JFrame {

public CounterFrame() {

JMenuBar menuBar = new JMenuBar();

JMenu menuFile = new JMenu();

JMenu menuFile1 = new JMenu();

JMenu menuFile2 = new JMenu();

JMenu menuFile3 = new JMenu();

JMenuItem menuFileExit = new JMenuItem();

menuFile.setText("文件");

menuFile1.setText("编辑");

menuFile2.setText("查看");

menuFile3.setText("帮助");

menuFileExit.setText("退出");

menuFileExit.addActionListener

(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

CounterFrame.this.windowClosed();

}

}

);

menuFile.add(menuFileExit);

menuBar.add(menuFile);

menuBar.add(menuFile1);

menuBar.add(menuFile2);

menuBar.add(menuFile3);

setTitle("计算器");

setJMenuBar(menuBar);

setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

CounterFrame.this.windowClosed();

}

}

);

}

protected void windowClosed() {

System.exit(0);

}

}

class Tool {

public static Tool instance;

private JTextField field;

private Tool() {

this.field=new JTextField(30);

this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()

{

if(instance==null)

{

instance=new Tool();

}

return instance;

}

public JTextField getfield()

{

return (this.field);

}

}

class Allpanel extends JPanel {

public Allpanel() {

this.setLayout(new BorderLayout(0,7));

Northpanel np=new Northpanel();

Centerpanel cp=new Centerpanel();

this.add(np,BorderLayout.NORTH);

this.add(cp,BorderLayout.CENTER);

}

}

class Centercenter extends JPanel {

static Vector Vec=new Vector();

static Vector vc=new Vector();

static Vector vc1=new Vector();

static Vector vc2=new Vector();

static Vector vc3=new Vector();

static String begin="yes";

static double add;

static double jq;

static double cs;

static double cq;

static double dy;

static String jg;

static String what;

static double tool=0;

static String to="yes";

/**

* Method Centercenter

*

*

*/

public Centercenter() {

// TODO: Add your code here

final JTextField text=Tool.getinstance().getfield();

this.setLayout(new GridLayout(4,5,3,3));

String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};

for(int i=0;i20;i++)

{

final JButton b=new JButton(arg[i]);

//this.add(new JButton(arg[i]));

this.add(b);

if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mark=b.getText();

String ma=text.getText();

if(vc3.contains("v3"))

{

text.setText("0."+mark);

vc3.clear();

}

else if(vc.contains("a"))

{

if(vc2.contains("v2"))

{

text.setText("0."+mark);

vc.clear();

vc2.clear();

}

else

{

text.setText(mark);

vc.clear();

Vec.clear();

Vec.add(mark);

}

}

else

{

text.setText(ma.trim()+mark);

Vec.add(mark);

}

begin="no";

to="yes";

}

});

}

if(i==17)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mar=b.getText();

String m=text.getText();

if("yes".equals(begin))

{

vc3.add("v3");

}

if(vc1.contains("v1"))

{

vc2.add("v2");

vc1.clear();

}

if(!Vec.contains(".")!vc.contains("a"))

{

text.setText(m.trim()+mar);

Vec.add(".");

}

}

});

}

if(i==18)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

add=Double.parseDouble(ma);

if(what==null)

{

tool=add;

what="add";

}

else

{

tool=tool+add;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="+";

}

});

}

if(i==13)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

jq=Double.parseDouble(ma);

if(what==null)

{

tool=jq;

what="jq";

}

else

{

tool=tool-jq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="-";

}

});

}

if(i==3)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

if(what==null)

{

tool=cq;

what="cq";

}

else

{

tool=tool/cq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="/";

}

});

}

if(i==4)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

text.setText(String.valueOf(Math.sqrt(cq)));

}

});

}

if(i==8)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cs=Double.parseDouble(ma);

if(what==null)

{

tool=cs;

what="cs";

}

else

{

tool=tool*cs;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="*";

}

});

}

if(i==19)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

dy=Double.parseDouble(ma);

if(what=="add")

{

jg=String.valueOf((tool+dy));

}

if(what=="jq")

{

jg=String.valueOf((tool-dy));

}

if(what=="cs")

{

jg=String.valueOf((tool*dy));

}

if(what=="cq")

{

jg=String.valueOf((tool/dy));

}

if(what==null)

{

if(to=="+")

{

tool=add;

jg=String.valueOf(tool+dy);

}

else if(to=="-")

{

tool=jq;

jg=String.valueOf(dy-tool);

}

else if(to=="*")

{

tool=cs;

jg=String.valueOf(dy*tool);

}

else if(to=="/")

{

tool=cq;

jg=String.valueOf(dy/tool);

}

else

{

jg=String.valueOf(dy);

}

}

text.setText(jg);

Vec.clear();

Vec.add(".");

vc.add("a");

vc1.add("v1");

what=null;

tool=0;

}

});

}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

JButton jb1=new JButton("Backspace");

JButton jb2=new JButton(" CE ");

JButton jb3=new JButton(" C ");

this.add(jb1);

this.add(jb2);

this.add(jb3);

jb1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

String back=Tool.getinstance().getfield().getText();

text.setText(backmethod(back));

Centercenter.Vec.remove(Centercenter.Vec.size()-1);

}

});

jb3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

text.setText("0.");

Centercenter.Vec.clear();

Centercenter.Vec.add(".");

Centercenter.vc.add("a");

Centercenter.begin="yes";

Centercenter.vc1.clear();

Centercenter.what=null;

Centercenter.tool=0;

}

});

}

public String backmethod(String str)

{

return str.substring(0,str.length()-1);

}

}

class Centerpanel extends JPanel {

public Centerpanel() {

this.setLayout(new BorderLayout(8,7));

Centernorth cn=new Centernorth();

Centercenter cc=new Centercenter();

Centerwest cw=new Centerwest();

this.add(cn,BorderLayout.NORTH);

this.add(cc,BorderLayout.CENTER);

this.add(cw,BorderLayout.WEST);

}

}

class Centerwest extends JPanel {

public Centerwest() {

this.setLayout(new GridLayout(4,1,3,3));

this.add(new JButton("MC"));

this.add(new JButton("MR"));

this.add(new JButton("MS"));

this.add(new JButton("M+"));

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {

tf=Tool.getinstance().getfield();

this.add(tf);

}

}

---------------------------------------------------------------------------

=============《按你要求特意后改过的最简单功能的代码如下》========================

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Counter2 {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();

}

}

class CounterFrame extends JFrame {

public CounterFrame() {

setTitle("计算器");

setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

CounterFrame.this.windowClosed();

}

}

);

}

protected void windowClosed() {

System.exit(0);

}

}

class Tool {

public static Tool instance;

private JTextField field;

private Tool() {

this.field=new JTextField(30);

this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()

{

if(instance==null)

{

instance=new Tool();

}

return instance;

}

public JTextField getfield()

{

return (this.field);

}

}

class Allpanel extends JPanel {

public Allpanel() {

this.setLayout(new BorderLayout(0,7));

Northpanel np=new Northpanel();

Centerpanel cp=new Centerpanel();

this.add(np,BorderLayout.NORTH);

this.add(cp,BorderLayout.CENTER);

}

}

class Centercenter extends JPanel {

static Vector Vec=new Vector();

static Vector vc=new Vector();

static Vector vc1=new Vector();

static Vector vc2=new Vector();

static Vector vc3=new Vector();

static String begin="yes";

static double add;

static double jq;

static double cs;

static double cq;

static double dy;

static String jg;

static String what;

static double tool=0;

static String to="yes";

/**

* Method Centercenter

*

*

*/

public Centercenter() {

// TODO: Add your code here

final JTextField text=Tool.getinstance().getfield();

this.setLayout(new GridLayout(4,5,3,3));

String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};

for(int i=0;i16;i++)

{

final JButton b=new JButton(arg[i]);

//this.add(new JButton(arg[i]));

this.add(b);

if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mark=b.getText();

String ma=text.getText();

if(vc3.contains("v3"))

{

text.setText("0."+mark);

vc3.clear();

}

else if(vc.contains("a"))

{

if(vc2.contains("v2"))

{

text.setText("0."+mark);

vc.clear();

vc2.clear();

}

else

{

text.setText(mark);

vc.clear();

Vec.clear();

Vec.add(mark);

}

}

else

{

text.setText(ma.trim()+mark);

Vec.add(mark);

}

begin="no";

to="yes";

}

});

}

if(i==14)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mar=b.getText();

String m=text.getText();

if("yes".equals(begin))

{

vc3.add("v3");

}

if(vc1.contains("v1"))

{

vc2.add("v2");

vc1.clear();

}

if(!Vec.contains(".")!vc.contains("a"))

{

text.setText(m.trim()+mar);

Vec.add(".");

}

}

});

}

if(i==15)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

add=Double.parseDouble(ma);

if(what==null)

{

tool=add;

what="add";

}

else

{

tool=tool+add;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="+";

}

});

}

if(i==11)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

jq=Double.parseDouble(ma);

if(what==null)

{

tool=jq;

what="jq";

}

else

{

tool=tool-jq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="-";

}

});

}

if(i==3)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

if(what==null)

{

tool=cq;

what="cq";

}

else

{

tool=tool/cq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="/";

}

});

}

if(i==7)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cs=Double.parseDouble(ma);

if(what==null)

{

tool=cs;

what="cs";

}

else

{

tool=tool*cs;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="*";

}

});

}

if(i==13)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

dy=Double.parseDouble(ma);

if(what=="add")

{

jg=String.valueOf((tool+dy));

}

if(what=="jq")

{

jg=String.valueOf((tool-dy));

}

if(what=="cs")

{

jg=String.valueOf((tool*dy));

}

if(what=="cq")

{

jg=String.valueOf((tool/dy));

}

if(what==null)

{

if(to=="+")

{

tool=add;

jg=String.valueOf(tool+dy);

}

else if(to=="-")

{

tool=jq;

jg=String.valueOf(dy-tool);

}

else if(to=="*")

{

tool=cs;

jg=String.valueOf(dy*tool);

}

else if(to=="/")

{

tool=cq;

jg=String.valueOf(dy/tool);

}

else

{

jg=String.valueOf(dy);

}

}

text.setText(jg);

Vec.clear();

Vec.add(".");

vc.add("a");

vc1.add("v1");

what=null;

tool=0;

}

});

}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

}

}

class Centerpanel extends JPanel {

public Centerpanel() {

this.setLayout(new BorderLayout(8,7));

Centernorth cn=new Centernorth();

Centercenter cc=new Centercenter();

Centerwest cw=new Centerwest();

this.add(cn,BorderLayout.NORTH);

this.add(cc,BorderLayout.CENTER);

this.add(cw,BorderLayout.WEST);

}

}

class Centerwest extends JPanel {

public Centerwest() {

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {

tf=Tool.getinstance().getfield();

this.add(tf);

}

}

------------------------------------------------------------

才子_辉祝您愉快!

java算24点代码:输入4个数算24点,能够在命令提示符下就可以运行。100多

import java.util.Scanner;

/** 给定4个数字计算24 */

public class Core {

private double expressionResult = 24;

// private int maxLine=10;

private boolean error = true;

private double numbers[] = new double[4];

public Object resultReturn;

/**

* 该对象拥有3个私有变量 expressionResult,所需结果 maxLine,输出结果每页行数 error,是否出错

* numbers[4],记录用来运算的4个数

*

* 其次,该对象拥有以下方法供外部调用 setNumbers(double[] 运算的数) 输入用来运算的数,4个时才能计算,无返回

* setMaxLine(int 行数) 输入每页的行数,无返回 getMaxLine() 返回每页的行数,类型为int

* setExpressionResult(double 所需结果) 输入所需结果,无返回 getExpressionResult()

* 返回所需结果,类型为double getExpression() 返回可得出所需结果的表达式,类型为字符串数组

*

* 最后,私有方法均为计算与表达式转换部分

*/

// 测试使用

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[] arr = new int[4];

System.out.print("输入第一个数:");

arr[0] = scanner.nextInt();

System.out.print("输入第二个数:");

arr[1] = scanner.nextInt();

System.out.print("输入第三个数:");

arr[2] = scanner.nextInt();

System.out.print("输入第四个数:");

arr[3] = scanner.nextInt();

Core s = new Core();

s.setNumbers(arr);

String[] output = s.getExpression();

for (int i = 0; i output.length; i++) {

System.out.println(output[i]);

}

}

/** 设定被计算的四个数,由于是数组,所以具有容错功能(不为4个数) */

public void setNumbers(double[] n) {

if (n.length == 4) {

error = false;

numbers = n;

} else

error = true;

}

public void setNumbers(int[] n) {

if (n.length == 4) {

error = false;

for (int i = 0; i 4; i++) {

numbers[i] = n[i];

}

} else

error = true;

}

/** 设定每页显示的行数 */

// public void setMaxLine(int n) {

// if (n0) {

// maxLine=n;

// }

// }

// /** 返回每页显示的行数 */

// public int getMaxLine() {

// return maxLine;

// }

/** 设定需要得到的结果 */

public void setExpressionResult(double n) {

expressionResult = n;

}

/** 返回所需结果 */

public double expressionResult() {

return expressionResult;

}

/** 返回符合条件的表达式 */

public String[] getExpression() {

if (!error) {

String[] expression = calculate(numbers);

return expression;

} else

return new String[] { "出错了,输入有误" };

}

/** cal24(),输出结果为24的表达式 */

private String[] calculate(double[] n) {

if (n.length != 4)

return new String[] { "Error" };

double[] n1 = new double[3];

double[] n2 = new double[2];

String[] resultString = new String[1024]; // 最多1000组解,暂时未溢出

int count = 0;

boolean isRepeat = false;

for (int t1 = 0; t1 6; t1++) {

for (int c1 = 0; c1 6; c1++) {

for (int t2 = 0; t2 3; t2++) {

for (int c2 = 0; c2 6; c2++) {

for (int c3 = 0; c3 6; c3++) {

if ((c1 / 3 == c2 / 3 (c1 % 3) * (c2 % 3) != 0)

|| (c2 / 3 == c3 / 3 (c2 % 3) * (c3 % 3) != 0)

|| (c1 / 3 == c3 / 3

(c1 % 3) * (c3 % 3) != 0 t2 == 2)) {

// 去除连减连除的解,因为x/(y/z)=x*z/y

continue;

}

n1 = cal1(n, t1, c1);

n2 = cal2(n1, t2, c2);

double result = cal(n2[0], n2[1], c3);

if ((result - expressionResult) 0.00000001

(expressionResult - result) 0.00000001) {

resultString[count] = calString(n, t1, c1, t2,

c2, c3)

+ "=" + (int) expressionResult;

for (int i = 0; i count; i++) {

isRepeat = false;

if (resultString[i]

.equals(resultString[count])) { // 去除完全重复的解

isRepeat = true;

break; // 提前退出循环

}

}

if (c1 == c2 c2 == c3 c1 % 3 == 0

t1 + t2 != 0) { // 连加连乘

isRepeat = true;

}

if (!isRepeat) {

count++;

}

}

}

}

}

}

}

if (count == 0)

return new String[] { "该组数无解" };

String[] resultReturn = new String[count];

System.arraycopy(resultString, 0, resultReturn, 0, count);

return resultReturn;

}

/** cal1(),将4个数计算一次后返回3个数 */

private double[] cal1(double[] n, int t, int c) { // t为原来的t1,c为原来的c1

double[] m = new double[3];

switch (t) {

case 0:

m[1] = n[2];

m[2] = n[3];

m[0] = cal(n[0], n[1], c);

break;

case 1:

m[1] = n[1];

m[2] = n[3];

m[0] = cal(n[0], n[2], c);

break;

case 2:

m[1] = n[1];

m[2] = n[2];

m[0] = cal(n[0], n[3], c);

break;

case 3:

m[1] = n[0];

m[2] = n[3];

m[0] = cal(n[1], n[2], c);

break;

case 4:

m[1] = n[0];

m[2] = n[2];

m[0] = cal(n[1], n[3], c);

break;

default:

m[1] = n[0];

m[2] = n[1];

m[0] = cal(n[2], n[3], c);

}

return m;

}

/** cal2(),将3个数计算一次后返回2个数 */

private double[] cal2(double[] n, int t, int c) { // t为原来的t2,c为原来的c2

double[] m = new double[2];

switch (t) {

case 0:

m[1] = n[2];

m[0] = cal(n[0], n[1], c);

break;

case 1:

m[1] = n[1];

m[0] = cal(n[0], n[2], c);

break;

default:

m[1] = n[0];

m[0] = cal(n[1], n[2], c);

}

return m;

}

/** cal(),将2个数计算后返回结果 */

private double cal(double n1, double n2, int c) { // n1,n2为运算数,c为运算类型

switch (c) {

case 0:

return n1 + n2;

case 1:

return n1 - n2;

case 2:

return n2 - n1;

case 3:

return n1 * n2;

case 4:

if (n2 == 0)

return 9999; // 使计算结果必不为24

else

return n1 / n2;

default:

if (n1 == 0)

return 9999; // 同上

else

return n2 / n1;

}

}

/** calString(),输出表达式 */

private String calString(double[] n, int t1, int c1, int t2, int c2, int c3) {

String[] nString = new String[4];

switch (t1) {

case 0:

nString[0] = calString2("" + (int) n[0], "" + (int) n[1], c1);

nString[1] = "" + (int) n[2];

nString[2] = "" + (int) n[3];

break;

case 1:

nString[0] = calString2("" + (int) n[0], "" + (int) n[2], c1);

nString[1] = "" + (int) n[1];

nString[2] = "" + (int) n[3];

break;

case 2:

nString[0] = calString2("" + (int) n[0], "" + (int) n[3], c1);

nString[1] = "" + (int) n[1];

nString[2] = "" + (int) n[2];

break;

case 3:

nString[0] = calString2("" + (int) n[1], "" + (int) n[2], c1);

nString[1] = "" + (int) n[0];

nString[2] = "" + (int) n[3];

break;

case 4:

nString[0] = calString2("" + (int) n[1], "" + (int) n[3], c1);

nString[1] = "" + (int) n[0];

nString[2] = "" + (int) n[2];

break;

default:

nString[0] = calString2("" + (int) n[2], "" + (int) n[3], c1);

nString[1] = "" + (int) n[0];

nString[2] = "" + (int) n[1];

}

if ((c2 / 3 c1 / 3 (t2 != 2 || c2 / 3 == c3 / 3))

|| ((c3 / 3 c1 / 3 + c2 / 3) t2 == 2)

|| (c3 == 1 c1 / 3 == 0)) // 特定情况下加上一个括号*****************************

nString[0] = '(' + nString[0] + ')';

switch (t2) {

case 0:

nString[0] = calString2(nString[0], "" + nString[1], c2);

nString[1] = nString[2];

break;

case 1:

nString[0] = calString2(nString[0], nString[2], c2);

break;

default:

nString[3] = nString[0];

nString[0] = calString2(nString[1], nString[2], c2);

nString[1] = nString[3];

}

if (c3 / 3 c2 / 3 || (c3 == 2 nString[0].indexOf('+') = 0)) // 特定情况下加上一个括号*****************************

nString[0] = '(' + nString[0] + ')';

return calString2(nString[0], nString[1], c3);

}

/** calString(),根据符号输出一部运算表达式 */

private String calString2(String n1, String n2, int c) {

switch (c) {

case 0:

return n1 + '+' + n2;

case 1:

return n1 + '-' + n2;

case 2:

return n2 + '-' + n1;

case 3:

return n1 + '*' + n2;

case 4:

return n1 + '/' + n2;

default:

return n2 + '/' + n1;

}

}

}

java代码中三个小数点连在一起表示什么

可变参数列表。一般用在最后一个参数上。比如public static void main(String... args),它就可以匹配许多参数调用。像main();

main(null);

main(null,null);

main(null,null,null);

......

main("a");

main("a","b");

main("a","b","c");

......

main(new String[]{});这些形状的调用都可以。一般在不固定参数输入时使用。比如一个exe程序执行时,你可以带若干个参数之类的。C里面的printf就是一个这样的参数,所以你打印时,可以在前面格式字串中用转义字符,后面跟相关的好几个参数。个数不限,可以是一个也可以是两个、三个……只要跟前面的转义字符匹配即可。


当前题目:java连点器代码 连点器的代码
转载来于:http://cqcxhl.cn/article/hjdies.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP