1.ç¨JAVAç¼åä¸ä¸ªè®¡ç®å¨
2.java编写程序:创建一个 计算器 的计算 界面,包含 0-9 个 数字 ,源源代 A-F个字母,码计码采用 GridLayout布局
3.java 写的算器计算器源代码只实现加减乘除四则运算即可
4.java怎样写一个简单的计算器
5.用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编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 ,计算 A-F个字母,源源代辩论系统源码采用 GridLayout布局
import java.awt.*;
import javax.swing.*;
public class MyCalculator extends JFrame{
public MyCalculator() {
String ss1 = "ABCDEF";
String[] ss2 = { "+",码计码"-","*","/","√ ̄","aⁿ","CE","="};//有些字符不在GBK中,所以文本使用了UTF-8编码。算器如果要用GBK编码,计算就把字符修改成汉字,源源代比如平方根,码计码乘方
JPanel jp0;//存放显示的算器文本框
JPanel jp1;//存放0~9 A~F
JPanel jp2;//存放运算符
//jp0
jp0 = new JPanel();
JTextField jtf = new JTextField();
jtf.setText("0");
jtf.setHorizontalAlignment(JTextField.RIGHT);//按照惯例,计算器的计算数字显示在右侧
jtf.setEditable(false);//按照惯例,计算器显示结果的源源代文本框不许编辑
jp0.add(jtf);
JButton jb;//按钮
//jp1
jp1 = new JPanel(new GridLayout(4, 4,3,3));
for (int i = 0; i < ss1.length(); i++) {
jb = new JButton(ss1.charAt(i)+"");
jp1.add(jb);
}
//jp2
jp2 = new JPanel(new GridLayout(3, 3,3,3));
for (int i = 0; i < ss2.length; i++) {
jb = new JButton(ss2[i]);
jb.setForeground(Color.BLUE);//为了突出这些运算符,把颜色调整成蓝色
jp2.add(jb);
}
add(jp0);
add(jp1);
add(jp2);
setLayout(new FlowLayout());//把JFrame设置成流式布局
setTitle("计算器");//设置窗口标题
setSize(,码计码影视分销系统 源码 );//设置窗口大小
setResizable(false);//设置窗口不许缩放,因为流式布局,缩放会导致布局变化
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置点击关闭退出jvm虚拟机
setVisible(true);//设置窗口可见
}
public static void main(String[] args) {
new MyCalculator();//创建窗口
}
}
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 符号键 次数 k2>1说明进行的是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(,);
JFrame frame=new JFrame("sunshine---计算器");
JTextField jg_TextField=new JTextField(jg,);//列
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(,);//设置清零按钮的大小 吗的 不好使 !!
frame.setLocation(, ); //主窗口 出现在位置
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= +5= 这种计算做准备
}
}
//----------------小数点 ---------------------
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;// 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怎样写一个简单的计算器
以下是一个简单的例子,这个计算器可以执行加、爱q社区源码减、乘、除操作:
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语句根据运算符执行相应的数学运算。最后,易语言web源码我们输出结果。
用JAVA编写的科学计算器源代码
以下是一个简单的用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!");
}
}
这个科学计算器支持基本的四则运算(加、减、乘、除)以及一些科学运算(正弦、余弦、正切、网页源码检查工具对数、指数和平方根)。用户可以通过输入相应的操作和两个数字来执行计算。例如,输入“add 2 3”将计算2加3的结果。
代码首先导入了`Scanner`类,用于从用户处获取输入。然后,在`main`方法中,创建了一个`Scanner`对象,用于读取用户的输入。程序通过一个无限循环来持续接收用户的输入,直到用户输入“quit”为止。
在循环中,程序首先提示用户输入一个操作,然后读取用户的输入并将其分割为多个部分。接着,程序将第二个和第三个部分转换为`double`类型的数字,并根据第一个部分(即操作)执行相应的计算。
程序使用`switch`语句来根据用户输入的操作执行相应的计算。对于基本的四则运算,程序直接执行相应的计算并输出结果。对于科学运算,程序使用了Java的`Math`类中的相应方法。例如,对于正弦运算,程序使用了`Math.sin`方法,并将角度转换为弧度作为参数传递给它。
如果用户输入了无效的操作或无效