Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Calculator of addition, subtraction, multiplication and Division in java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to add, subtract, multiply and divide calculators, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows

Code

Import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.awt.FlowLayout;import javax.swing.*;@SuppressWarnings ("unused") class Sumjp {JOptionPane jp = new JOptionPane (); Sumjp (String str) {JOptionPane.showMessageDialog (null, str); System.exit (0);} Sumjp (String [] str) {}} public class JiSuan extends JFrame implements ActionListener,ItemListener {/ * / private static final long serialVersionUID = 1L; double x1verse x2 Magazine sum = 0 String f; JTextField txt = new JTextField (30); JTextField txt1 = new JTextField (5); JTextField txt2 = new JTextField (5); JTextField txt3 = new JTextField (5); JLabel lb1 = new JLabel ("data 1:"); JLabel lb2 = new JLabel ("symbol:"); JLabel lb3 = new JLabel ("data 2:"); JButton jbtn = new JButton ("OK"); JiSuan () {setSize (350150); setVisible (true); setTitle ("Calculator:"); setDefaultCloseOperation (EXIT_ON_CLOSE) SetLayout (new FlowLayout ()); add (lb1); add (txt1); / / add (lb2); / / add (txt2); JComboBox jc = new JComboBox (); jc.addItem ("plus"); jc.addItem ("minus"); jc.addItem ("multiply"); jc.addItem ("divide"); add (jc); add (lb3); add (txt3); add (jbtn); add (txt); validate (); jc.addItemListener (this) Jbtn.addActionListener (this);} public void itemStateChanged (ItemEvent ie) {f = (String) ie.getItem (). ToString ();} public void actionPerformed (ActionEvent e) {x1 = Double.parseDouble (txt1.getText ()); / / f = txt2.getText (); x2 = Double.parseDouble (txt3.getText ()); if (f.equals ("plus")) {sum = x1 + x2;} if (f.equals (minus)) {sum = x1-x2 } if (f.equals ("multiplication")) {sum = x1x2;} if (f.equals (")) {sum = x1ax x2;} txt.setText (x1 + f + x2 +" = "+ sum); new Sumjp (x1 + f + x2 +" = "+ sum);} public static void main (String [] args) {/ / TODO auto-generated method stub new JiSuan ();}

The editor would like to share a piece of code for you. Thank the author for sharing: use java syntax to make a simple calculator of addition, subtraction, multiplication and division:

/ * implementation ideas: 1. Select all data to enter 2 from the keyboard. Use the switch statement to judge 3. Need to enter three times from the console * first number * operator * the second number is finally displayed on the console: welcome to the simple calculator system: please enter the first number: 10 Please enter the operator: + Please enter the second number: 20 calculation result: 10 "20" 30 s.nextInt () * / public class Calculator {public static void main (String [] args) {java.util.Scanner s = new java.util.Scanner (System.in); System.out.println ("Welcome to simple Calculator"); System.out.print ("Please enter the first number:"); int num1 = s.nextInt (); System.out.print ("Please enter operator:"); String operator = s.next () System.out.print ("Please enter the second number:"); int num2 = s.nextInt (); int result = 0; switch (operator) {case "+": result = num1 + num2; break; case "-": result = num1-num2; break; case "*": result = num1 * num2; break; case "/": result = num1 * num2; break; case "%": result = num1% num2 } System.out.println (num1+operator+num2+ "=" + result);}} above is all the content of the article "how to add, subtract, multiply and divide calculators in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report