×

java简易实现加减乘除代码

java简易实现加减乘除代码(java中简单的加减乘除运算)

admin admin 发表于2023-08-26 08:32:07 浏览32 评论0

抢沙发发表评论

本文目录

java中简单的加减乘除运算

在a.jsp中写如下代码:请输入《form action=“b.jsp“ method=“post“》上课作业比率:《input type=“text“ name=“zy“ /》 《br /》上课实训:《input type=“text“ name=“sksx“ /》 《br /》平时考勤: 《input type=“text“ name=“kq“ /》 《br /》平时实训:《input type=“text“ name=“pssx“ /》 《br /》《/form》b.jsp中..《%String s = request.getParameter(“zy“)//.......%》懒得写了太麻烦@_@

java做一个简单的加减乘除运算,用键盘先后输入a,b的数值

//参考下我刚写的这个,稍显得智能一些!你可以稍加更改;思路都注释的很详细了!//代码也不算多,import java.util.Scanner;public class 计算器 { private static Scanner sc = new Scanner(System.in); public static void main(String args) { init(); } private static void init() { while (true) sop(input()); } private static String input() { sop(“请输入运算表达式如:1+1回车!“); String str = sc.nextLine(), sum = ““; char tem = 0; int poin = 0; for (int i = 0; i 《 str.length(); i++) {//遍历! char cs = str.charAt(i); if (cs == ’+’ || cs == ’-’ || cs == ’*’ || cs == ’/’) {// 寻找找运算符号 tem = str.charAt(i);// 记录运算符号! poin = i;// 记录符号位置! break; } else if (i 》= str.length() - 1) { sop(“输入不合法请检查!“); init(); } }// 拆分数字! double a = Double.parseDouble(str.substring(0, poin)); double b = Double.parseDouble(str.substring(poin + 1)); switch (tem) {// 开始判断运算! case ’+’: sum = ““ + (a + b); break; case ’-’: sum = ““ + (a - b); break; case ’*’: sum = ““ + (a * b); break; default: sum = ““ + (a / b); break; } return str + “=“ + sum; }//打印功能! private static void sop(Object obj) { System.out.println(obj); }}

java加减乘除运算的程序怎么编

//JAVA编程:四则运算(接收用户输入的2个操作数,和运算符),计算之后,输出结果~~~~import java.util.Scanner;public class 四则运算 { public static void main(String args) { Scanner sc = new Scanner(System.in); System.out.print(“请输入第一个数字:“); int a = sc.nextInt(); System.out.print(“请输入运算符号:“); String str = sc.next(); char ch = str.charAt(0); System.out.print(“请输入第二个数字:“); int b = sc.nextInt(); switch(ch) { case ’+’: System.out.println(a+“+“+ b + “=“+(a+b)); break; case ’-’: System.out.println(a+“-“+ b+ “=“+(a-b)); break; case ’*’: System.out.println(a+“*“+ b+ “=“+(a*b)); break; case ’/’: if(b==0){ System.out.println(“被除数为零,运算无意义!“); break; } else { System.out.println(a+“/“+ b+ “ = “+(a/b)); break; } default: System.out.println(“运算符是无意义字符!“); break; } }} 望采纳~~~~~~~~

如何使用Java对象语言编写一个加减乘除计算器要有代码

下面文件名要为:JiSuanQi.java

import java.awt.*;

import java.awt.event.*;

public class JiSuanQi

{

   String s=““,s1=null,s2=null;

   Frame f=new Frame(“计算器“);

  TextField tf=new TextField(30);

  Panel p1=new Panel();

  Panel p2=new Panel();

  Panel p3=new Panel();

  Button bt1=new Button(“=“);

  Button bt2=new Button(“删除“);

  Button bt=new Button;

  int id=0;

   public static void main(String args)

   {

       new JiSuanQi().init();

   }

    public void init()

    {

       f.setBackground(new Color(85,247,253));

       f.setLayout(new BorderLayout(4,4));

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

       p3.setLayout(new BorderLayout(4,4));

       f.setResizable(false);

       f.add(p1,BorderLayout.NORTH);

       f.add(p2);

       p3.add(bt2,BorderLayout.NORTH);

       p3.add(bt1);

       p1.add(tf);

       f.add(p3,BorderLayout.EAST);

       String b={“1“,“2“,“3“,“+“,“4“,“5“,“6“,“-“,“7“,“8“,“9“,“*“,“0“,“.“,“复位“,“/“};

       for(int i=0;i《16;i++)

       {

           bt);

           p2.add(bt);

       }

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.blue);

       bt.setForeground(Color.red);

       bt.setForeground(Color.red);

       bt.setForeground(Color.red);

       bt.setForeground(Color.red);

       bt.setForeground(Color.red);

       bt1.setForeground(Color.red);

       bt2.setForeground(Color.red);

       f.pack();

       f.setVisible(true);

       f.addWindowListener(new WindowAdapter()

       {

          public void windowClosing(WindowEvent e)

          {

             System.exit(0); 

          } 

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=1;

                 s2+=1;

                 tf.setText(s);

           }

       }

       );

          bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=2;

                 s2+=2;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=3;

                 s2+=3;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=4;

                 s2+=4;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=5;

                 s2+=5;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=6;

                 s2+=6;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=7;

                 s2+=7;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=8;

                 s2+=8;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=9;

                 s2+=9;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=0;

                 s2+=0;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=’.’;

                 s2+=’.’;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+=’+’;

                 id=1;

                 s2=““;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+=’-’;

                 id=2;

                 s2=““;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+=’*’;

                 id=3;

                 s2=““;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s=““;

                 s2=““;

                 tf.setText(s);

           }

       }

       );

       bt.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+=’/’;

                 id=4;

                 s2=““;

                 tf.setText(s);

           }

       }

       );

       bt1.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 if(id《1) ;

                    else{

                     s+=’=’;

                     double a=Double.parseDouble(s1);

                     double b=Double.parseDouble(s2);

                     double c=0;

                     switch(id)

                     {

                       case 1:c=a+b; break;

                       case 2:c=a-b; break;

                       case 3:c=a*b; break;

                       case 4:c=a/b; break;

                     }             

                   s+=c;  

                   tf.setText(s);

                   }          

                 s=““;s1=““;s2=““;id=0;

           }

       }

       );

       bt2.addActionListener(new ActionListener()

       {

               public void actionPerformed(ActionEvent e)

               { 

             char c1;

             char c2=new char;

             c1=s.toCharArray();

             for(int i=0;i《s.length()-1;i++)

                 c2;

             s=s.valueOf(c2); 

             

             

             if(id《1)

             {

                s1=s;

             }

            if(s2.length()》=1)

            {

             char c3;

             char c4=new char;   

             c3=s2.toCharArray();

             for(int i=0;i《s2.length()-1;i++)

             c4;

             s2=s2.valueOf(c4);   

             } 

                    tf.setText(s);   

               }

       }

       );

    }

}

java编程简易计算器功能的模拟,完成加减乘除操作

源代码发给你参考下

import java.awt.*;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.border.EmptyBorder;public class JiSuanQi extends JFrame implements ActionListener { private JPanel cp; private JTextField jtf3; private JTextField jtf1; private JTextField jtf2; public static void main(String args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JiSuanQi frame = new JiSuanQi(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public JiSuanQi() { setTitle(“简易计算器“); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(335, 185); setLocationRelativeTo(null); cp = new JPanel(); cp.setBorder(new EmptyBorder(5, 5, 5, 5)); cp.setLayout(new GridLayout(3, 1)); setContentPane(cp); JPanel jp1 = new JPanel(); JLabel jl1 = new JLabel(“操作数1“); JLabel jl2 = new JLabel(“操作数2“); jtf1 = new JTextField(6); jtf2 = new JTextField(6); jp1.add(jl1); jp1.add(jtf1); jp1.add(jl2); jp1.add(jtf2); JPanel jp2 = new JPanel(); JButton jb1 = new JButton(“加“); jb1.addActionListener(this); JButton jb2 = new JButton(“减“); jb2.addActionListener(this); JButton jb3 = new JButton(“乘“); jb3.addActionListener(this); JButton jb4 = new JButton(“除“); jb4.addActionListener(this); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); jp2.add(jb4); JPanel jp3 = new JPanel(); JLabel jl4 = new JLabel(“运行结果:“); jtf3 = new JTextField(12); jp3.add(jl4); jp3.add(jtf3); cp.add(jp1); cp.add(jp2); cp.add(jp3); } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); String result ; double d1 = Double.parseDouble(jtf1.getText()); double d2 = Double.parseDouble(jtf2.getText()); if (command.equals(“加“)) { result = (d1+d2)+““; } else if (command.equals(“减“)) { result = (d1-d2)+““; } else if (command.equals(“乘“)) { result = (d1*d2)+““; } else if(command.equals(“除“)){ result = (d1/d2)+““; }else{ result = “Error“; } jtf3.setText(result); }}

JAVA制作一个简单的计算器能实现两个数的加、减、乘、除四则运算

import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.applet.*;public class Demo03 extends JApplet implements ActionListener { boolean i = true; private JButton num0 = new JButton(“0“); private JButton num1 = new JButton(“1“); private JButton num2 = new JButton(“2“); private JButton num3 = new JButton(“3“); private JButton num4 = new JButton(“4“); private JButton num5 = new JButton(“5“); private JButton num6 = new JButton(“6“); private JButton num7 = new JButton(“7“); private JButton num8 = new JButton(“8“); private JButton num9 = new JButton(“9“); private JButton zuok = new JButton(“(“); private JButton youk = new JButton(“)“); private JButton dian = new JButton(“.“); private JButton NULL = new JButton(“N“); private JButton plu = new JButton(“+“); private JButton min = new JButton(“-“); private JButton mul = new JButton(“x“); private JButton div = new JButton(“/“); private JButton equ = new JButton(“=“); private JButton cle = new JButton(“C“);// 清除 private JTextField space = new JTextField(15); public void init() { JPanel text = new JPanel(); text.setLayout(new FlowLayout()); text.add(space); JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(5, 4)); buttons.add(num9); buttons.add(num8); buttons.add(num7); buttons.add(plu); buttons.add(num6); buttons.add(num5); buttons.add(num4); buttons.add(min); buttons.add(num3); buttons.add(num2); buttons.add(num1); buttons.add(mul); buttons.add(num0); buttons.add(cle); buttons.add(equ); buttons.add(div); (num9).addActionListener(this); (num8).addActionListener(this); (num7).addActionListener(this); (num6).addActionListener(this); (num5).addActionListener(this); (num4).addActionListener(this); (num3).addActionListener(this); (num2).addActionListener(this); (num1).addActionListener(this); (num0).addActionListener(this); (plu).addActionListener(this); (min).addActionListener(this); (mul).addActionListener(this); (div).addActionListener(this); (equ).addActionListener(this); (cle).addActionListener(this); setLayout(new BorderLayout()); add(“North“, text); add(“South“, buttons); space.setText(“0“); } public void actionPerformed(ActionEvent e) { if (e.getSource() == num9) { if (i == true) { space.setText(“9“); i = false; } else space.setText(space.getText() + ’9’); } if (e.getSource() == num8) { if (i == true) { space.setText(“8“); i = false; } else space.setText(space.getText() + ’8’); } if (e.getSource() == num7) { if (i == true) { space.setText(“7“); i = false; } else space.setText(space.getText() + ’7’); } if (e.getSource() == num6) { if (i == true) { space.setText(“6“); i = false; } else space.setText(space.getText() + ’6’); } if (e.getSource() == num5) { if (i == true) { space.setText(“5“); i = false; } else space.setText(space.getText() + ’5’); } if (e.getSource() == num4) { if (i == true) { space.setText(“4“); i = false; } else space.setText(space.getText() + ’4’); } if (e.getSource() == num3) { if (i == true) { space.setText(“3“); i = false; } else space.setText(space.getText() + ’3’); } if (e.getSource() == num2) { if (i == true) { space.setText(“2“); i = false; } else space.setText(space.getText() + ’2’); } if (e.getSource() == num1) { if (i == true) { space.setText(“1“); i = false; } else space.setText(space.getText() + ’1’); } if (e.getSource() == num0) { if (i == true) { space.setText(“0“); i = false; } else space.setText(space.getText() + ’0’); } if (e.getSource() == plu) { space.setText(space.getText() + ’+’); i = false; } if (e.getSource() == min) { space.setText(space.getText() + ’-’); i = false; } if (e.getSource() == mul) { space.setText(space.getText() + ’*’); i = false; } if (e.getSource() == div) { space.setText(space.getText() + ’/’); i = false; } if (e.getSource() == equ) { space.setText(String.valueOf(Calculator(space.getText()))); i = true; } if (e.getSource() == cle) { space.setText(“0“); i = true; } } public double Calculator(String f)// 科学计算 { int i = 0, j = 0, k; char c; StringBuffer s = new StringBuffer(); s.append(f); s.append(’=’); String formula = s.toString(); char anArray; anArray = new char; Stack《Character》 mystack = new Stack《Character》(); while (formula.charAt(i) != ’=’) { c = formula.charAt(i); switch (c) { case ’+’: case ’-’: while (!mystack.empty() && mystack.peek().charValue() != ’(’) { anArray; } } if (c == ’#’) mystack1.push(new Double(d)); i++; break; } } return (mystack1.peek().doubleValue()); }}

你看看这个合适不?

怎么用java编程,实现分数的加减乘除运算

    java编程实现分数的加减乘除运算的步骤如下:

    1、打开eclipse,创建一个Java工程,在此工程里新建一个类;

    2、在新建的类中,添加4个运算类;

    3、在主方法中调用对应的方法即可完成分数的加减乘除运算了。

    具体实现代码如下:

     public class Demo {    public static void main(String args) {        System.out.println(jia(1, 2));        System.out.println(jian(1, 2));        System.out.println(cheng(1, 2));        System.out.println(chu(1, 2));    }    //加法运算    private static float jia(float x,float y) {        return x + y;    }    //减法运算    private static float jian(float x,float y) {        return x - y;    }    //乘法运算    private static float cheng(float x,float y) {        return x * y;    }    //除法运算    private static float chu(float x,float y) {        return x / y;    }}

如何用java编写一个简单的计算器,只要能进行最基本的加减乘除运算就可以了,本人急用,简单易懂即可,大

简单的啊,我有个自己编的完美的,不过给你改成简单的吧。有注释。import java.awt.*;import javax.swing.*;import javax.swing.border.*;import java.awt.event.*;import java.text.*;public class Calculator extends JFrame implements ActionListener { int m=0,i,n=0; float sum=0,s1,s2,equ; String v=““; JPanel delete_main = new JPanel(); JPanel delete = new JPanel(); JPanel buttons = new JPanel(); JTextField text = new JTextField(“0“); JButton backspace = new JButton(“Backspace“); JButton c = new JButton(“C“); JButton ce = new JButton(“CE“); JButton bt1= new JButton(“1“); JButton bt2= new JButton(“2“); JButton bt3= new JButton(“3“); JButton bt4= new JButton(“+“); JButton bt5 = new JButton(“4“); JButton bt6= new JButton(“5“); JButton bt7= new JButton(“6“); JButton bt8= new JButton(“-“); JButton bt9= new JButton(“7“); JButton bt10= new JButton(“8“); JButton bt11= new JButton(“9“); JButton bt12= new JButton(“*“); JButton bt13= new JButton(“0“); JButton bt14= new JButton(“.“); JButton bt15= new JButton(“=“); JButton bt16= new JButton(“/“); public Calculator() { super(“简易计算器--超人不会飞荣誉出品“); delete.setLayout(new GridLayout(1, 3, 15, 15)); delete.add(backspace); delete.add(c); delete.add(ce); delete.setBorder(new LineBorder(delete.getBackground(), 5));//添加边框 c.addActionListener(this); ce.addActionListener(this); backspace.addActionListener(this); text.setFont(new Font(“宋体“, Font.BOLD +Font.ITALIC, 20)); // 设置显示字体 text.setBackground(Color.getHSBColor(44, 3, 87)); text.setBorder(new LineBorder(Color.ORANGE, 1)); text.setHorizontalAlignment(SwingConstants.RIGHT); // 设置鼠标靠右 text.setEditable(false); // 屏蔽键盘输入,防止非法字符 delete_main.setLayout(new GridLayout(2, 1, 10, 10)); delete_main.add(text, BorderLayout.NORTH); delete_main.add(delete, BorderLayout.SOUTH); buttons.setLayout(new GridLayout(4, 5, 10, 20)); bt1.addActionListener(this); // 为各个按钮添加事件监听 buttons.add(bt1); bt2.addActionListener(this); buttons.add(bt2); bt3.addActionListener(this); buttons.add(bt3); bt4.addActionListener(this); buttons.add(bt4); bt5.addActionListener(this); buttons.add(bt5); bt6.addActionListener(this); buttons.add(bt6); bt7.addActionListener(this); buttons.add(bt7); bt8.addActionListener(this); buttons.add(bt8); bt9.addActionListener(this); buttons.add(bt9); bt10.addActionListener(this); buttons.add(bt10); bt11.addActionListener(this); buttons.add(bt11); bt12.addActionListener(this); buttons.add(bt12); bt13.addActionListener(this); buttons.add(bt13); bt14.addActionListener(this); buttons.add(bt14); bt15.addActionListener(this); buttons.add(bt15); bt16.addActionListener(this); buttons.add(bt16); buttons.setBorder(new LineBorder(delete.getBackground(), 5));//边框 this.setLayout(new BorderLayout()); this.add(delete_main, BorderLayout.NORTH); this.add(buttons, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加关闭窗口事件监听器 pack(); //自动设置窗体大小 setResizable(false); // 设置面板大小不可变 setVisible(true); Toolkit tk = this.getToolkit(); Dimension de = tk.getScreenSize(); setBounds((de.width - this.getWidth()) / 2,(de.height - this.getHeight()) / 2, this.getWidth(), this.getHeight()); //使之居于屏幕正中央 } public void actionPerformed(ActionEvent e) { //事件响应 //Object source = e.getSource(); if(e.getSource()==backspace) //退格键 { v=v.substring(0,v.length() - 1); text.setText(v); } if(e.getSource()==c||e.getSource()==ce) //清空键 { m=0; sum=0; v=““;s1=0;s2=0;i=0; text.setText(“0“); } if(e.getSource()==bt1) { if(m==1) v=““; v=v+“1“; text.setText(v); m=0;n=1; } if(e.getSource()==bt2) { if(m==1) v=““; v=v+“2“; text.setText(v); m=0;n=1; } if(e.getSource()==bt3) { if(m==1) v=““; v=v+“3“; text.setText(v); m=0;n=1; } if(e.getSource()==bt4) // +按钮 { if(n==1) //如果按之前按了0~9数字键,就运算 { sum=Float.parseFloat(v); } else //如果按之前没按0~9数字键,直接按了运算符,不做任何操作 { } i=1; m=1;n=0; } if(e.getSource()==bt5) { if(m==1) v=““; v=v+“4“; text.setText(v); m=0;n=1; } if(e.getSource()==bt6) { if(m==1) v=““; v=v+“5“; text.setText(v); m=0;n=1; } if(e.getSource()==bt7) { if(m==1) v=““; v=v+“6“; text.setText(v); m=0;n=1; } if(e.getSource()==bt8) // -按钮 { if(n==1) { sum=Float.parseFloat(v); } else { } i=2; m=1;n=0; } if(e.getSource()==bt9) { if(m==1) v=““; v=v+“7“; text.setText(v); m=0;n=1; } if(e.getSource()==bt10) { if(m==1) v=““; v=v+“8“; text.setText(v); m=0;n=1; } if(e.getSource()==bt11) { if(m==1) v=““; v=v+“9“; text.setText(v); m=0;n=1; } if(e.getSource()==bt12) //*按钮 { if(n==1) { sum=Float.parseFloat(v); } else { } i=3; m=1;n=0; } if(e.getSource()==bt13) { if(m==1) v=““; v=v+“0“; text.setText(v); m=0;n=1; } if(e.getSource()==bt14) { } if(e.getSource()==bt15) //等于按钮 { if(m==1) // 如果输入了+=等非法输入,不做任何操作 { } else { s1=sum; s2=Float.parseFloat(v); switch(i) { case 0: //如果按过数字键后直接按等号,直接输出 equ=Float.parseFloat(v); break; case 1: equ=s1+s2; break; case 2: equ=s1-s2; break; case 3: equ=s1*s2; break; case 4: equ=s1/s2; break; } sum=0;v=““; //清空运算数 v=String.valueOf(equ); text.setText(String.valueOf(equ)); i=0; } } if(e.getSource()==bt16) // 除按钮 { if(n==1) { sum=Float.parseFloat(v); } else { } i=4; m=1;n=0; } } public static void main(String args) { Calculator bt=new Calculator(); }}