java数字计算代码 java计算数字位数
java编一个计算器的代码
界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
成都创新互联公司长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为宁县企业提供专业的成都网站制作、成都网站建设、外贸营销网站建设,宁县网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
代码如下:
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写计算器代码加减乘除
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator()
{
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
java 算数代码
pselect user.id, user.name, buy.id, buy.time from user left outer join buy on user.id = buy.userid order by buy.time asc group by buy.userid;/pp算法这题题目有点问题,每种奖品的数量都知道了,难道所有奖品的总数量会不知道吗?所以这个count我只能理解为,需要抽出的奖品的数量,即抽奖几次。/pp割一下-----------------------------------------------------------------/pp又仔细看了一下题目,发现最终返回的是一个String,也就是只抽一次奖,具体要根据每种奖品的数量不同来计算概率进行抽奖,count的确是所有奖品的数量,但是我从map把每种奖品的总数累加,不就是所有奖品数量了嘛。。。传个count确实是多此一举了
public static String draw(MapString, Integer map, int count) {
// 产生1-count之间的随机数
Integer random = new Random().nextInt(count) + 1;
// map的key,即奖品名称
SetString keys = map.keySet();
// map的value值,即每种奖品的数量
CollectionInteger values = map.values();
// 题目明确说了有3种奖品,获取前两种奖品的数量
Integer count1 = (int) values.toArray()[0];
Integer count2 = (int) values.toArray()[1];
String name = null;// 奖品名称
if (random = count1) {// 表示抽中第一种奖品
name = (String) keys.toArray()[0];
} else if (random count1 + count2) {// 表示抽中第三种奖品
name = (String) keys.toArray()[2];
} else {// 表示抽中第二中奖品
name = (String) keys.toArray()[1];
}
return name;
}
Java中能求出任意3个数字中最大值的代码吗?
Java中能求出任意3个数字中最大值的代码
1、if语句嵌套
2、if语句
3、if语句(假定a最大,b,c与a比较,如果比a大,则赋值给max)
4、三元运算符
5、if语句 + 逻辑运算符 (a,b,c三个数,如果不是a最大,或者b最大,就是c最大)
一、if语句嵌套
int a = 10;
int b = 30;
int c = 20;
int max;
if (a b) {
if (a c) {
max = a;
} else {
max = c;
}
} else {
if (b c) {
max = b;
} else {
max = c;
}
}
二、if语句
int a = 10;
int b = 30;
int c = 20;
int max;
if (a b) {
max = a;
} else {
max = b;
}
if (max c) {
max = c;
}
三、if语句(假定a最大,b,c与a比较,如果比a大,则赋值给max)
int a = 10;
int b = 30;
int c = 20;
int max = a;
if (b max) {
max = b;
}
if (c max) {
max = c;
}
四、三元运算符
int a = 10;
int b = 30;
int c = 20;
int max = (a b) ? a : b;
max = (max c) ? max : c;
或者
int max = ((a b ? a : b) c) ? (a b ? a : b) : c;(建议不用这种)
五、if语句 + 逻辑运算符 (a,b,c三个数,如果不是a最大,或者b最大,就是c最大)
int a = 10;
int b = 30;
int c = 20;
int max;
if (a b a c) {
max = a;
} else if (c a c b) {
max = c;
} else
max = b;
当前标题:java数字计算代码 java计算数字位数
当前链接:http://pwwzsj.com/article/ddjgpjo.html