真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java加法計(jì)算器代碼 java計(jì)算器程序代碼實(shí)現(xiàn)加減乘除

用java寫個(gè)簡(jiǎn)單的加減計(jì)算器

不僅可以加減還可以乘除,代碼如下:

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括新區(qū)網(wǎng)站建設(shè)、新區(qū)網(wǎng)站制作、新區(qū)網(wǎng)頁制作以及新區(qū)網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,新區(qū)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到新區(qū)省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

package 計(jì)算器;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

boolean flag = false;

JTextField jtf = new JTextField();

public static void main(String[] args) {

new Calculator();

}

public Calculator() {

setSize(300, 300);

setTitle("計(jì)算器");

Container c = getContentPane();

jtf.setHorizontalAlignment(JTextField.RIGHT);

JButton jb = new JButton("=");

jb.addActionListener(this);

JPanel jp = new JPanel();

c.add(jtf, BorderLayout.NORTH);

c.add(jp, BorderLayout.CENTER);

c.add(jb, BorderLayout.SOUTH);

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

addButton(jp);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

setVisible(true);

}

public void addButton(JPanel jp) {

JButton b = null;

b = new JButton("1");

b.addActionListener(this);

jp.add(b);

b = new JButton("2");

b.addActionListener(this);

jp.add(b);

b = new JButton("3");

b.addActionListener(this);

jp.add(b);

b = new JButton("+");

b.addActionListener(this);

jp.add(b);

b = new JButton("4");

b.addActionListener(this);

jp.add(b);

b = new JButton("5");

b.addActionListener(this);

jp.add(b);

b = new JButton("6");

b.addActionListener(this);

jp.add(b);

b = new JButton("-");

b.addActionListener(this);

jp.add(b);

b = new JButton("7");

b.addActionListener(this);

jp.add(b);

b = new JButton("8");

b.addActionListener(this);

jp.add(b);

b = new JButton("9");

b.addActionListener(this);

jp.add(b);

b = new JButton("*");

b.addActionListener(this);

jp.add(b);

b = new JButton("0");

b.addActionListener(this);

jp.add(b);

b = new JButton(".");

b.addActionListener(this);

jp.add(b);

b = new JButton("C");

b.addActionListener(this);

jp.add(b);

b = new JButton("/");

b.addActionListener(this);

jp.add(b);

}

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand() == "=") {

if (jtf.getText().matches("\\d+\\.?\\d*[\\+\\-\\*\\/]\\d+\\.?\\d*")) {

String s = jtf.getText();

Pattern p = Pattern.compile("\\d+\\.?\\d*");

Matcher m = p.matcher(s);

m.find();

double firstNum = Double.parseDouble(m.group());

m.find();

double secondNum = Double.parseDouble(m.group());

if (jtf.getText().matches("\\d+\\.?\\d*\\+\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum + secondNum));

} else if (jtf.getText().matches("\\d+\\.?\\d*\\-\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum - secondNum));

} else if (jtf.getText().matches("\\d+\\.?\\d*\\*\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum * secondNum));

} else if (jtf.getText().matches("\\d+\\.?\\d*\\/\\d+\\.?\\d*")) {

jtf.setText("" + (firstNum / secondNum));

}

} else {

JOptionPane.showMessageDialog(null, "請(qǐng)輸入正確表達(dá)式!");

}

flag = true;

} else if (e.getActionCommand() == "C") {

jtf.setText("");

} else {

if (flag) {

if (e.getActionCommand() != "+" e.getActionCommand() != "-"

e.getActionCommand() != "*"

e.getActionCommand() != "/") {

//System.out.println(e.getActionCommand() != "+");

jtf.setText("");

}

flag = false;

}

jtf.setText(jtf.getText() + e.getActionCommand());

}

}

}

編寫java程序簡(jiǎn)單計(jì)算器

主要涉及的知識(shí)點(diǎn): 類的寫法, 以及方法的調(diào)用 .建議多做練習(xí). 如果有看不懂的地方. 可以繼續(xù)追問,一起討論.

參考代碼如下

//Number類

class?Number?{

private?int?n1;//私有的整型數(shù)據(jù)成員n1

private?int?n2;//私有的整型數(shù)據(jù)成員n2

//?通過構(gòu)造函數(shù)給n1和n2賦值

public?Number(int?n1,?int?n2)?{

this.n1?=?n1;

this.n2?=?n2;

}

//?加法

public?int?addition()?{

return?n1?+?n2;

}

//?減法

public?int?subtration()?{

return?n1?-?n2;

}

//?乘法

public?int?multiplication()?{

return?n1?*?n2;

}

//?除法?(可能除不盡,所以使用double作為返回類型)

public?double?division()?{

return?n1?*?1.0?/?n2;?//?通過n1*1.0?把計(jì)算結(jié)果轉(zhuǎn)換成double類型.

}

}

//Exam4?類

public?class?Exam4{

public?static?void?main(String[]?args)?{

Number?number=new?Number(15,?6);//創(chuàng)建Number類的對(duì)象

//下面的是調(diào)用方法得到返回值進(jìn)行輸出顯示

System.out.println("加法"+number.addition());

System.out.println("減法"+number.subtration());

System.out.println("乘法"+number.multiplication());

System.out.println("除法"+number.division());

}

}

java加法計(jì)算器代碼

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {

initComponents();

}

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

num1 = new javax.swing.JTextField();

num2 = new javax.swing.JTextField();

result = new javax.swing.JTextField();

addBtn = new javax.swing.JButton();

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

jMenu3 = new javax.swing.JMenu();

jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Num1:");

jLabel2.setText("Num2:");

jLabel3.setText("Num3:");

addBtn.setText("Add");

addBtn.addActionListener(new jisuanAC());

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(53, 53, 53)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(addBtn)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(31, 31, 31)

.addComponent(num1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel2)

.addComponent(jLabel3))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(result)

.addComponent(num2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))

.addGap(168, 168, 168))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(19, 19, 19)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(num2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(61, 61, 61)

.addComponent(addBtn)

.addContainerGap(81, Short.MAX_VALUE))

);

jMenu1.setText("Operation");

jMenu3.setText("Add");

jMenu1.add(jMenu3);

jMenuBar1.add(jMenu1);

jMenu2.setText("Exit");

jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}// /editor-fold

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setVisible(true);

}

});

}

private class jisuanAC implements ActionListener

{

public void actionPerformed(ActionEvent e) {

if(e.getSource()== addBtn)

{

int number1 = Integer.parseInt(num1.getText());

int number2 = Integer.parseInt(num2.getText());

int rs = number1 + number2;

result.setText(String.valueOf(rs));

}

}

}

// Variables declaration - do not modify

private javax.swing.JButton addBtn;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenu jMenu2;

private javax.swing.JMenu jMenu3;

private javax.swing.JMenuBar jMenuBar1;

private javax.swing.JPanel jPanel1;

private javax.swing.JTextField num1;

private javax.swing.JTextField num2;

private javax.swing.JTextField result;

}

這是只有一個(gè)加法的例子!希望幫到你


當(dāng)前名稱:java加法計(jì)算器代碼 java計(jì)算器程序代碼實(shí)現(xiàn)加減乘除
轉(zhuǎn)載來源:http://weahome.cn/article/dohgiii.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部