计算器代码(Java)

package jyh;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class helloa extends Applet implements ActionListener
{
 String x="",y="",symbol="";
 double answer;
 JTextField tfAnswer; 
 Button button[]=new Button[10];
 Button Jia,Jian,Cheng,Chu,ReStart,Pow,Sqrt,Deng,Fu,Point,sin,cos,tan,ln;
 public void init()
 {
  this.setBackground(Color.CYAN);  
  setLayout(null);
  tfAnswer=new JTextField();
  tfAnswer.setBounds(5, 5, 265, 30);
  tfAnswer.setHorizontalAlignment(JTextField.RIGHT); 
  tfAnswer.setText("0.");
  add(tfAnswer);
  SetButton(button[9],"9",95,50,40,30,Color.blue);
  SetButton(button[8],"8",50,50,40,30,Color.blue);
  SetButton(button[7],"7",5,50,40,30,Color.blue);
  SetButton(button[6],"6",95,85,40,30,Color.blue);
  SetButton(button[5],"5",50,85,40,30,Color.blue);
  SetButton(button[4],"4",5,85,40,30,Color.blue);
  SetButton(button[3],"3",95,120,40,30,Color.blue);
  SetButton(button[2],"2",50,120,40,30,Color.blue);
  SetButton(button[1],"1",5,120,40,30,Color.blue);
  SetButton(button[0],"0",5,155,40,30,Color.blue);
  SetButton(Jia,"+",140,155,40,30,Color.red);
  SetButton(Jian,"-",140,120,40,30,Color.red);
  SetButton(Cheng,"*",140,85,40,30,Color.red);
  SetButton(Chu,"/",140,50,40,30,Color.red);
  SetButton(ReStart,"ReStart",185,50,40,30,Color.red);
  SetButton(Pow,"^",185,85,40,30,Color.blue);
  SetButton(Sqrt,"sqrt",185,120,40,30,Color.blue);
  SetButton(Deng,"=",185,155,40,30,Color.red);
  SetButton(Fu,"+/-",50,155,40,30,Color.blue);
  SetButton(Point,".",95,155,40,30,Color.blue);
  SetButton(sin, "sin", 230, 50, 40, 30, Color.red);
  SetButton(cos, "cos", 230, 85, 40, 30, Color.red);
  SetButton(tan, "tan", 230, 120, 40, 30, Color.red);
  SetButton(ln, "ln", 230, 155, 40, 30, Color.red);
 }



 public void SetButton(Button name,String symbol,int x,int y,int width,int height,Color color)
 {
  setLayout(null);
  name=new Button(symbol);
  name.setBounds(x,y,width,height);
  name.setBackground(Color.white);
  name.setForeground(color);
  name.addActionListener(this);
  add(name);
 }


 public void Equal(String z)
 {
  if(z.equals("+")) 
      answer=Double.parseDouble(x)+Double.parseDouble(y);
  if(z.equals("-")) 
      answer=Double.parseDouble(x)-Double.parseDouble(y);
  if(z.equals("*")) 
      answer=Double.parseDouble(x)*Double.parseDouble(y);
  if(z.equals("/")) 
      answer=Double.parseDouble(x)/Double.parseDouble(y);
  if(z.equals("^"))
      answer=Math.pow(Double.parseDouble(x),Double.parseDouble(y));
  if(z.equals("sin"))
      answer=Math.sin(Double.parseDouble(x));
  if(z.equals("cos"))
      answer=Math.cos(Double.parseDouble(x));
  if(z.equals("tan"))
      answer=Math.tan(Double.parseDouble(x));
  if(z.equals("ln"))
      answer=Math.log(Double.parseDouble(x));
  x=Double.toString(answer);
  tfAnswer.setText(x);
  y="";symbol="";
 }

 public void actionPerformed(ActionEvent e) throws IndexOutOfBoundsException
 {
  if (
    e.getActionCommand().equals("0")||
    e.getActionCommand().equals("1")||
    e.getActionCommand().equals("2")||
    e.getActionCommand().equals("3")||
    e.getActionCommand().equals("4")||
    e.getActionCommand().equals("5")||
    e.getActionCommand().equals("6")||
    e.getActionCommand().equals("7")||
    e.getActionCommand().equals("8")||
    e.getActionCommand().equals("9")
    )
    {
         if (symbol.equals(""))
         {
             x=x+e.getActionCommand();
                 if (x.startsWith("00"))
                 {
                  x=x.substring(1);
                 }
             tfAnswer.setText(x);
         }
         else
         {
             y=y+e.getActionCommand();
                 if (y.startsWith("00"))
                 {
                  y=y.substring(1);
                 }
            tfAnswer.setText(y);
         }
    }

  if (e.getActionCommand().equals("."))
  {
   if (symbol.equals(""))
   {
    int i=0,j=0;
        for (i=0;i<x.length();i++)
        {
             if (x.charAt(i)=='.')
             {
              j++;
             }
        }
        if (j==0)
        {
         x=x+".";
        }
    tfAnswer.setText(x);
   }
   else
   {
       int i=0,j=0;
            for (i=0;i<y.length();i++)
            {
                 if (y.charAt(i)=='.')
                 {
                  j++;
                 }
            }
             if (j==0)
             {
              y=y+".";
             }
    tfAnswer.setText(y);
   }
  }

  if (e.getActionCommand().equals("ReStart"))
  {
       x="";y="";symbol="";
       tfAnswer.setText("0.");
  }  
  if (e.getActionCommand().equals("+/-"))
  {
       if (symbol.equals(""))
       {
            if(x.substring(0,1).equals("-"))
            {
             x=x.substring(1);
            }
            else
            {
             x="-"+x;
            }
            tfAnswer.setText(x);
       }
   else
   {
        if(y.substring(0,1).equals("-"))
        {
            y=y.substring(1);
        }
        else
        {
            y="-"+y;
        }

        tfAnswer.setText(y);
   }
  }

  if(e.getActionCommand().equals("sqrt"))
  {
       if(symbol!="") 
       {
           Equal(symbol);
       }
       answer=Math.sqrt(Double.parseDouble(x));
       x=Double.toString(answer);
       tfAnswer.setText(x);
  }  
  if(e.getActionCommand().equals("+"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="+";
  }
  if(e.getActionCommand().equals("-"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="-";
  }
  if(e.getActionCommand().equals("*"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="*";
  }
  if(e.getActionCommand().equals("/"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="/";
  }
  if(e.getActionCommand().equals("^"))
  {
   if(symbol!="") 
       Equal(symbol);
   symbol="^";
  }
  if(e.getActionCommand().equals("sin"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.sin(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("ln"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.log(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("cos"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.cos(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("tan"))
  {
      if(symbol!="")
          Equal(symbol);
      answer=Math.tan(Double.parseDouble(x));
      x=Double.toString(answer);
      tfAnswer.setText(x);
  }
  if(e.getActionCommand().equals("=")) 
      Equal(symbol);
 }
}
### 回答1: 好的,下面是一个简易计算器Java 代码示例: ```java public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public double divide(int a, int b) { if (b == 0) { throw new IllegalArgumentException("Divisor cannot be zero"); } return (double) a / b; } } ``` 在这个示例中,我们创建了一个名为 `Calculator` 的类,其中包含了加法、减法、乘法和除法四种运算方法。其中,加法、减法和乘法的实现比较简单,直接使用了 Java 语言中的加、减、乘运算符;而除法的实现稍微复杂一些,需要判断除数是否为零,如果为零则抛出异常。 ### 回答2: 下面是一个简易计算器Java代码示例: ```java import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个数字: "); double num1 = input.nextDouble(); System.out.print("请输入第二个数字: "); double num2 = input.nextDouble(); System.out.println("请选择要进行的操作:"); System.out.println("1. 加法"); System.out.println("2. 减法"); System.out.println("3. 乘法"); System.out.println("4. 除法"); System.out.print("请输入选项(1-4): "); int option = input.nextInt(); switch (option) { case 1: System.out.println("结果: " + (num1 + num2)); break; case 2: System.out.println("结果: " + (num1 - num2)); break; case 3: System.out.println("结果: " + (num1 * num2)); break; case 4: if (num2 != 0) { System.out.println("结果: " + (num1 / num2)); } else { System.out.println("除数不能为0"); } break; default: System.out.println("无效选项"); } } } ``` 以上代码通过Scanner类接收用户输入的两个数字和操作选项,然后通过switch语句根据选项对两个数字进行相应的运算,并输出结果。如果选项为除法操作并且除数为0,则会输出"除数不能为0"的提示信息。 ### 回答3: 简易计算器是我们日常生活中常用的工具,用于进行基本的数学运算。以下是使用Java编写的简易计算器代码: ```java import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个数字:"); double num1 = input.nextDouble(); System.out.print("请输入运算符(+、-、*、/):"); String operator = input.next(); System.out.print("请输入第二个数字:"); double num2 = input.nextDouble(); double result = 0; boolean isValidOperator = true; switch(operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": if(num2 != 0) { result = num1 / num2; } else { System.out.println("除数不能为0!"); isValidOperator = false; } break; default: System.out.println("无效的运算符!"); isValidOperator = false; break; } if(isValidOperator) { System.out.println("计算结果是:" + result); } } } ``` 使用该简易计算器,用户需要依次输入第一个数字、运算符和第二个数字。程序将根据用户输入的运算符执行相应的运算,并输出结果。如果输入的运算符不合法,程序会给出相应的提示信息。如果用户尝试进行除法运算时,除数为0,程序也会给出相应的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值