1.ç¨JAVAç¼åä¸ä¸ªè®¡ç®å¨
2.javaç¼ä¸ä¸ªè®¡ç®å¨ç代ç
3.java怎样写一个简单的计算计算器
4.用JAVA编写的科学计算器源代码
ç¨JAVAç¼åä¸ä¸ªè®¡ç®å¨
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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Jisuanqi extends JFrame implements ActionListener {
/
***
*/
private static final long serialVersionUID = 1L;
Result result = new Result(); // å®ä¹textçé¢æ¿
Number_Key number_key = new Number_Key(); // å®ä¹æé®é¢æ¿
// å½ç¹å»æé®+ã-ã*ã/æ¶ï¼com = true
boolean com = false;
// å½i=0æ¶è¯´ææ¯æ们第ä¸æ¬¡è¾å ¥ï¼å符串textä¸ä¼ç´¯å
int i = 0;
// åæ¾textçå 容
String text = "";
// åæ¾ç¹å»æé®+ã-ã*ã/ä¹åçæ°å¼
double defbutton = 0;
// +ã-ã*ã/ç代å·åå«ä¸º1ï¼2ï¼3ï¼4
int symbol = 0;
// æé å½æ°
Jisuanqi() {
super("WangJiao"); // 设å®æ é¢
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设å®å ³éçªä½æ¶éåºç¨åº
JPanel pane = new JPanel(); // å®ä¹ä¸»é¢æ¿
pane.setLayout(new BorderLayout());
setBounds(, , , ); // å两个åæ°æ¯å¨å±å¹ä¸æ¾ç¤ºçåæ ï¼å两个æ¯å¤§å°
// æ¿æ¢å¾æ
ImageIcon icon = new ImageIcon("F:1.GIF");
// Jisuanqi.class.getResource("APPLE.GIF")
// );
setIconImage(icon.getImage());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.CENTER);
pane.add(number_key.equal, BorderLayout.SOUTH);
number_key.one.addActionListener(this); // 对1æé®æ·»å çå¬äºä»¶
number_key.two.addActionListener(this); // 对2æé®æ·»å çå¬äºä»¶
number_key.three.addActionListener(this); // 对3æé®æ·»å çå¬äºä»¶
number_key.four.addActionListener(this); // 对4æé®æ·»å çå¬äºä»¶
number_key.five.addActionListener(this); // 对5æé®æ·»å çå¬äºä»¶
number_key.six.addActionListener(this); // 对6æé®æ·»å çå¬äºä»¶
number_key.seven.addActionListener(this); // 对7æé®æ·»å çå¬äºä»¶
number_key.eight.addActionListener(this); // 对8æé®æ·»å çå¬äºä»¶
number_key.nine.addActionListener(this); // 对9æé®æ·»å çå¬äºä»¶
number_key.zero.addActionListener(this); // 对0æé®æ·»å çå¬äºä»¶
number_key.ce.addActionListener(this); // 对置é¶æé®æ·»å çå¬äºä»¶
number_key.plus.addActionListener(this); // 对+æé®æ·»å çå¬äºä»¶
number_key.equal.addActionListener(this); // 对=æé®æ·»å çå¬äºä»¶
number_key.sub.addActionListener(this); // 对-æé®æ·»å çå¬äºä»¶
number_key.mul.addActionListener(this); // 对*æé®æ·»å çå¬äºä»¶
number_key.div.addActionListener(this); // 对/æé®æ·»å çå¬äºä»¶
number_key.point.addActionListener(this); // 对.æé®æ·»å çå¬äºä»¶
setContentPane(pane);
pack(); // åå§åçªä½å¤§å°ä¸ºæ£å¥½çæ¾æææé®
}
// å个æé®è§¦åçäºä»¶
public void actionPerformed(ActionEvent e) {
/
** å¦ææ¯ç¹å»æ°åæé®é£ä¹å è¦å¤ææ¯å¦å¨æ¤ä¹åç¹å»äº+ã-ã*ã/ã=ï¼å¦ææ¯é£ä¹com=true å¦æ没æcom=
* false;æè æ¯å¦ç¹å»æ°åé®ï¼å¦ææ¯i = 1,å¦æ没æ i = 0;
*/
if (e.getSource() == number_key.one) {
if (com || i == 0) {
result.text.setText("1");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "1");
}
} else if (e.getSource() == number_key.two) {
if (com || i == 0) {
result.text.setText("2");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "2");
}
} else if (e.getSource() == number_key.three) {
if (com || i == 0) {
result.text.setText("3");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "3");
}
} else if (e.getSource() == number_key.four) {
if (com || i == 0) {
result.text.setText("4");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "4");
}
} else if (e.getSource() == number_key.five) {
if (com || i == 0) {
result.text.setText("5");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "5");
}
} else if (e.getSource() == number_key.six) {
if (com || i == 0) {
result.text.setText("6");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "6");
}
} else if (e.getSource() == number_key.seven) {
if (com || i == 0) {
result.text.setText("7");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "7");
}
} else if (e.getSource() == number_key.eight) {
if (com || i == 0) {
result.text.setText("8");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "8");
}
} else if (e.getSource() == number_key.nine) {
if (com || i == 0) {
result.text.setText("9");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "9");
}
}
/
** 对äº0è¿ä¸ªæé®æä¸å®ç说æ³ï¼å¨æçç¨åºéä¸ä¼åºç°å¦è¿æ ·çæ åµï¼æå äºå¤ææ¡ä»¶å°±æ¯
* å¦ætextä¸çæ°å¼=0å°±è¦å¤æå¨è¿ä¸ªæ°å¼ä¸æ¯å¦æ.åå¨?å¦ææé£ä¹å°±å¨åæ¥æ°å¼åºç¡ä¹ä¸æ·» å 0;å¦åä¿æåæ¥çæ°å¼ä¸å
*/
else if (e.getSource() == number_key.zero) { // result.text.getText()æ¯å¾å°textéå 容çææ
if (com || i == 0) {
result.text.setText("0");
com = false;
i = 1;
} else {
text = result.text.getText();
if (Float.parseFloat(text) > 0 || Float.parseFloat(text) < 0) { // Float.parseFloat(text)å°±æ¯ç±»å转æ¢äºï¼ä¸é¢é½æ¯ä¸æ ·
result.text.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
result.text.setText(text);
} else {
result.text.setText(text + "0");
}
}
}
} else if (e.getSource() == number_key.ce) {
result.text.setText("0");
i = 0;
com = true;
// text = "";
defbutton = 0;
}
/
** æ¬ç¨åºä¸ä¼è®©ä¸ä¸ªæ°å¼ä¸åºç°2个以ä¸çå°æ°ç¹.å ·ä½åæ³æ¯:å¤ææ¯å¦å·²ç»åå¨.åå¨å°±ä¸æ·»å , ä¸åå¨å°±æ·»å .
*/
else if (e.getSource() == number_key.point) {
if (com || i == 0) {
result.text.setText("0.");
com = false;
i = 1;
} else {
text = result.text.getText();
if (text.trim().indexOf(".") == -1) {
result.text.setText(text + ".");
} else {
result.text.setText(text);
}
}
} // è·å¾ç¹å»+ä¹åçæ°å¼
else if (e.getSource() == number_key.plus) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 1;
} // è·å¾ç¹å»-ä¹åçæ°å¼
else if (e.getSource() == number_key.sub) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 2;
} // è·å¾ç¹å»*ä¹åçæ°å¼
else if (e.getSource() == number_key.mul) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol = 3;
} // è·å¾ç¹å»/ä¹åçæ°å¼
else if (e.getSource() == number_key.div) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 4;
} else if (e.getSource() == number_key.equal) {
switch (symbol) {
case 1: { // 计ç®å æ³
double ad = defbutton
+ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 2: { // 计ç®åæ³
double ad = defbutton
- Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
text = "";
break;
}
case 3: { // 计ç®ä¹æ³
double ad = defbutton
* Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 4: { // 计ç®é¤æ³
double ad = defbutton
/ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Jisuanqi loveyou = new Jisuanqi();
loveyou.show();
}
}
// 计ç®å¨æ°åæé®å®ä¹é¢æ¿
class Number_Key extends JPanel {
/
***
*/
private static final long serialVersionUID = 1L;
JButton zero = new JButton("0"); // æ°åé®0
JButton one = new JButton("1"); // æ°åé®1
JButton two = new JButton("2"); // æ°åé®2
JButton three = new JButton("3"); // æ°åé®3
JButton four = new JButton("4"); // æ°åé®4
JButton five = new JButton("5"); // æ°åé®5
JButton six = new JButton("6"); // æ°åé®6
JButton seven = new JButton("7"); // æ°åé®7
JButton eight = new JButton("8"); // æ°åé®8
JButton nine = new JButton("9"); // æ°åé®9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("æ¸ é¶"); // ç½®é¶é®
JButton point = new JButton(".");
Number_Key() {
setLayout(new GridLayout(4, 4, 1, 1)); // å®ä¹å¸å±ç®¡çå¨ä¸ºç½æ ¼å¸å±
setBackground(Color.blue); // 设置èæ¯é¢è²
// æ·»å æé®
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
// 计ç®å¨æ¾ç¤ºç»æççªä½
class Result extends JPanel {
/
***
*/
private static final long serialVersionUID = 1L;
// textå æ¯è¾å ¥åç»æ
JTextField text = new JTextField("0");
@SuppressWarnings("deprecation")
Result() { // 讲è¾å ¥çæ°åæå¾å°çç»æå¨textçå³è¾¹æ¾ç¤º
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false); // ææ¬æ¡ä¸è½ç¼è¾
setLayout(new BorderLayout()); // 设å®å¸å±ç®¡çå¨è¾¹æ¡å¸å±
add(text, BorderLayout.CENTER); // textæ¾ç½®å¨çªä½çä¸é´
}
}
ç´æ¥å¤å¶ ä¿åæJisuanqi .javaå¯ä»¥ç´æ¥è¿è¡äº
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(, ));
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();
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;i<;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==||i==||i==||i==)
{
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==)
{
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==)
{
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==)
{
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==)
{
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(, ));
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();
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;i<;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==||i==)
{
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==)
{
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==)
{
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==)
{
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==)
{
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怎样写一个简单的计算器
以下是一个简单的例子,这个计算器可以执行加、器源减、码计码乘、算器推箱子c 源码除操作:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number:");
double num1 = scanner.nextDouble();
System.out.println("Enter second number:");
double num2 = scanner.nextDouble();
System.out.println("Enter an operator (+,源代 -, *, /):");
char operator = scanner.next().charAt(0);
scanner.close();
double output;
switch(operator)
{
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
if (num2 != 0) {
output = num1 / num2;
} else {
System.out.println("Error! Dividing by zero is not allowed.");
return;
}
break;
default:
System.out.println("Error! Invalid operator. Only +, -, * and / are allowed.");
return;
}
System.out.println("The result is given as follows:");
System.out.printf("%.1f %c %.1f = %.1f\n", num1, operator, num2, output);
}
}
在这个程序中,我们首先使用Scanner类从用户那里获取两个数字和一个运算符。计算然后,器源我们使用switch语句根据运算符执行相应的码计码数学运算。最后,算器我们输出结果。源代
用JAVA编写的计算whmcs快捷登录源码科学计算器源代码
以下是一个简单的用Java编写的科学计算器的源代码示例:
java
import java.util.Scanner;
public class ScientificCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Scientific Calculator!");
System.out.println("Enter 'add', 'subtract', 'multiply', 'divide', 'sin', 'cos', 'tan', 'log', 'exp', 'sqrt', or 'quit' to exit.");
while (true) {
System.out.print("Enter operation (e.g., add 2 3): ");
String operation = scanner.nextLine();
if (operation.equalsIgnoreCase("quit")) {
break;
}
String[] parts = operation.split(" ");
double num1 = Double.parseDouble(parts[1]);
double num2 = Double.parseDouble(parts[2]);
switch (parts[0].toLowerCase()) {
case "add":
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
break;
case "subtract":
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
break;
case "multiply":
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
break;
case "divide":
if (num2 != 0) {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
case "sin":
System.out.println("sin(" + num1 + ") = " + Math.sin(Math.toRadians(num1)));
break;
case "cos":
System.out.println("cos(" + num1 + ") = " + Math.cos(Math.toRadians(num1)));
break;
case "tan":
System.out.println("tan(" + num1 + ") = " + Math.tan(Math.toRadians(num1)));
break;
case "log":
System.out.println("log(" + num1 + ") = " + Math.log(num1));
break;
case "exp":
System.out.println("exp(" + num1 + ") = " + Math.exp(num1));
break;
case "sqrt":
if (num1 >= 0) {
System.out.println("sqrt(" + num1 + ") = " + Math.sqrt(num1));
} else {
System.out.println("Error: Cannot calculate the square root of a negative number.");
}
break;
default:
System.out.println("Error: Invalid operation.");
break;
}
}
scanner.close();
System.out.println("Goodbye!");
}
}
这个科学计算器支持基本的四则运算(加、减、器源乘、码计码除)以及一些科学运算(正弦、算器余弦、源代正切、go编译源码包对数、指数和平方根)。用户可以通过输入相应的操作和两个数字来执行计算。例如,输入“add 2 3”将计算2加3的源码时代内部问题结果。
代码首先导入了`Scanner`类,用于从用户处获取输入。然后,在`main`方法中,创建了一个`Scanner`对象,autojs游戏辅助源码用于读取用户的输入。程序通过一个无限循环来持续接收用户的输入,直到用户输入“quit”为止。
在循环中,程序首先提示用户输入一个操作,然后读取用户的输入并将其分割为多个部分。接着,程序将第二个和第三个部分转换为`double`类型的数字,并根据第一个部分(即操作)执行相应的计算。
程序使用`switch`语句来根据用户输入的操作执行相应的计算。对于基本的四则运算,程序直接执行相应的计算并输出结果。对于科学运算,程序使用了Java的`Math`类中的相应方法。例如,对于正弦运算,程序使用了`Math.sin`方法,并将角度转换为弧度作为参数传递给它。
如果用户输入了无效的操作或无效