欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

用java代碼寫加減乘除,java加減乘除代碼怎么寫

求簡單java寫計算器代碼加減乘除

import javax.swing.*;

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的西秀網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

import java.awt.*;

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener

{

private boolean dotExist, operated, equaled; // 幫助運算的布爾變量

private double storedNumber; // 目前的結(jié)果

private char lastOperator; // 表示上一運算符

private JTextField operation; // 結(jié)果欄

private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 運算符

private JButton[] numbers; // 數(shù)字

// 構(gòu)造者

public Calculator()

{

setTitle("Calculator");

// 初始化變量

dotExist = false; // 表示當(dāng)前的數(shù)是否有小數(shù)點

operated = false; // 表示任意運算符是否被按下

equaled = false; // 表示等號是否被按下

storedNumber = 0;

lastOperator = '?';

// 初始化窗口變量

operation = new JTextField("0");

operation.setEditable(false);

numbers = new JButton[10];

for (int i = 0; i 10; i++)

numbers[i] = new JButton("" + i);

dot = new JButton(".");

plus = new JButton("+");

minus = new JButton("-");

multi = new JButton("*");

div = new JButton("/");

sqrt = new JButton("√");

equal = new JButton("=");

changePN = new JButton("±");

clear = new JButton("AC");

// 將窗口物體放入窗口

GridBagLayout layout = new GridBagLayout();

getContentPane().setLayout(layout);

addComponent(layout, operation, 0, 0, 4, 1);

addComponent(layout, numbers[1], 1, 0, 1, 1);

addComponent(layout, numbers[2], 1, 1, 1, 1);

addComponent(layout, numbers[3], 1, 2, 1, 1);

addComponent(layout, numbers[4], 2, 0, 1, 1);

addComponent(layout, numbers[5], 2, 1, 1, 1);

addComponent(layout, numbers[6], 2, 2, 1, 1);

addComponent(layout, numbers[7], 3, 0, 1, 1);

addComponent(layout, numbers[8], 3, 1, 1, 1);

addComponent(layout, numbers[9], 3, 2, 1, 1);

addComponent(layout, dot, 4, 0, 1, 1);

addComponent(layout, numbers[0], 4, 1, 1, 1);

addComponent(layout, sqrt, 4, 2, 1, 1);

addComponent(layout, plus, 1, 3, 1, 1);

addComponent(layout, minus, 2, 3, 1, 1);

addComponent(layout, multi, 3, 3, 1, 1);

addComponent(layout, div, 4, 3, 1, 1);

addComponent(layout, equal, 5, 0, 2, 1);

addComponent(layout, changePN, 5, 2, 1, 1);

addComponent(layout, clear, 5, 3, 1, 1);

}

// 對按鈕進行反應(yīng)的方法

public void actionPerformed(ActionEvent e)

{

JButton btn = (JButton)e.getSource();

if (btn == clear)

{

operation.setText("0");

dotExist = false;

storedNumber = 0;

lastOperator = '?';

}

else if (btn == equal)

{

operate('=');

equaled = true;

}

else if (btn == plus)

{

operate('+');

equaled = false;

}

else if (btn == minus)

{

operate('-');

equaled = false;

}

else if (btn == multi)

{

operate('*');

equaled = false;

}

else if (btn == div)

{

operate('/');

equaled = false;

}

else if (btn == changePN)

{

operate('p');

operate('=');

equaled = true;

}

else if (btn == sqrt)

{

operate('s');

operate('=');

equaled = true;

}

else

{

if (equaled)

storedNumber = 0;

for (int i = 0; i 10; i++)

if (btn == numbers[i])

{

if (operation.getText().equals("0"))

operation.setText("" + i);

else if(! operated)

operation.setText(operation.getText() + i);

else

{

operation.setText("" + i);

operated = false;

}

}

if (btn == dot ! dotExist)

{

operation.setText(operation.getText() + ".");

dotExist = true;

}

}

}

// 進行運算的方法

private void operate(char operator)

{

double currentNumber = Double.valueOf(operation.getText()).doubleValue();

if (lastOperator == '?')

storedNumber = currentNumber;

else if (lastOperator == '+')

storedNumber += currentNumber;

else if (lastOperator == '-')

storedNumber -= currentNumber;

else if (lastOperator == '*')

storedNumber *= currentNumber;

else if (lastOperator == '/')

storedNumber /= currentNumber;

else if (lastOperator == 'p')

storedNumber *= -1;

else if (lastOperator == 's')

storedNumber = Math.sqrt(currentNumber);

else if (lastOperator == '=' equaled)

storedNumber = currentNumber;

operation.setText("" + storedNumber);

operated = true;

lastOperator = operator;

}

// 快捷使用GridBagLayout的方法

private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)

{

GridBagConstraints constraints = new GridBagConstraints();

constraints.fill = GridBagConstraints.BOTH;

constraints.insets = new Insets(10, 2, 10, 2);

constraints.weightx = 100;

constraints.weighty = 100;

constraints.gridx = col;

constraints.gridy = row;

constraints.gridwidth = width;

constraints.gridheight = height;

layout.setConstraints(component, constraints);

if (component instanceof JButton)

((JButton)component).addActionListener(this);

getContentPane().add(component);

}

// 主方法初始化并顯示窗口

public static void main(String[] args)

{

Calculator calc = new Calculator();

calc.setSize(290, 400);

calc.setVisible(true);

}

}

如何使用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[16];

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",".","復(fù)位","/"};

for(int?i=0;i16;i++)

{

bt[i]=new?Button(b[i]);

p2.add(bt[i]);

}

bt[0].setForeground(Color.blue);

bt[1].setForeground(Color.blue);

bt[2].setForeground(Color.blue);

bt[4].setForeground(Color.blue);

bt[5].setForeground(Color.blue);

bt[6].setForeground(Color.blue);

bt[8].setForeground(Color.blue);

bt[9].setForeground(Color.blue);

bt[10].setForeground(Color.blue);

bt[12].setForeground(Color.blue);

bt[13].setForeground(Color.blue);

bt[3].setForeground(Color.red);

bt[7].setForeground(Color.red);

bt[11].setForeground(Color.red);

bt[15].setForeground(Color.red);

bt[14].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[0].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=1;

s2+=1;

tf.setText(s);

}

}

);

bt[1].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=2;

s2+=2;

tf.setText(s);

}

}

);

bt[2].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=3;

s2+=3;

tf.setText(s);

}

}

);

bt[4].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=4;

s2+=4;

tf.setText(s);

}

}

);

bt[5].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=5;

s2+=5;

tf.setText(s);

}

}

);

bt[6].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=6;

s2+=6;

tf.setText(s);

}

}

);

bt[8].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=7;

s2+=7;

tf.setText(s);

}

}

);

bt[9].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=8;

s2+=8;

tf.setText(s);

}

}

);

bt[10].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=9;

s2+=9;

tf.setText(s);

}

}

);

bt[12].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+=0;

s2+=0;

tf.setText(s);

}

}

);

bt[13].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s+='.';

s2+='.';

tf.setText(s);

}

}

);

bt[3].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s1=s;

s+='+';

id=1;

s2="";

tf.setText(s);

}

}

);

bt[7].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s1=s;

s+='-';

id=2;

s2="";

tf.setText(s);

}

}

);

bt[11].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s1=s;

s+='*';

id=3;

s2="";

tf.setText(s);

}

}

);

bt[14].addActionListener(new?ActionListener()

{

public?void?actionPerformed(ActionEvent?e)

{

s="";

s2="";

tf.setText(s);

}

}

);

bt[15].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(id1)?;

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[s.length()-1];

c1=s.toCharArray();

for(int?i=0;is.length()-1;i++)

c2[i]=c1[i];

s=s.valueOf(c2);

if(id1)

{

s1=s;

}

if(s2.length()=1)

{

char[]?c3;

char[]?c4=new?char[s2.length()-1];

c3=s2.toCharArray();

for(int?i=0;is2.length()-1;i++)

c4[i]=c3[i];

s2=s2.valueOf(c4);

}

tf.setText(s);

}

}

);

}

}

用Java編寫實現(xiàn)加減乘除,界面如下

用Java編寫的實現(xiàn)加減乘除的程序如下

import?java.awt.BorderLayout;

import?java.awt.event.ActionEvent;

import?java.awt.event.ActionListener;

import?javax.swing.JButton;

import?javax.swing.JComboBox;

import?javax.swing.JFrame;

import?javax.swing.JLabel;

import?javax.swing.JOptionPane;

import?javax.swing.JPanel;

import?javax.swing.JTextField;

public?class?Calculator?extends?JFrame?implements?ActionListener{

JLabel?jl1=new?JLabel("第一個數(shù)");

JLabel?jl2=new?JLabel("運算符");

JLabel?jl3=new?JLabel("第二個數(shù)");

JLabel?jl4=new?JLabel("結(jié)果:");

JTextField?jtf1=new?JTextField(8);

JTextField?jtf2=new?JTextField(8);

JTextField?jtf3=new?JTextField(8);

String?a[]={"+","-","*","/"};

JComboBoxString?jcb=new?JComboBoxString(a);

JButton?jb1=new?JButton("計算");

JButton?jb2=new?JButton("退出");

JPanel?jp=new?JPanel();

Calculator(){

setTitle("計算器");

jb1.addActionListener(this);

jb2.addActionListener(this);

jp.setLayout(null);

jl1.setBounds(30,?30,?80,?20);

jl2.setBounds(110,?30,?80,?20);

jl3.setBounds(190,?30,?80,?20);

jtf1.setBounds(30,?60,?70,?20);

jcb.setBounds(110,?60,?70,20);

jtf2.setBounds(190,?60,?70,?20);

jl4.setBounds(80,?110,?40,?20);

jtf3.setBounds(110,?110,?100,?20);

jb1.setBounds(60,?160,?60,?25);

jb2.setBounds(170,?160,?60,?25);

jp.add(jl1);jp.add(jl2);jp.add(jl3);

jp.add(jtf1);jp.add(jcb);jp.add(jtf2);

jp.add(jl4);jp.add(jtf3);

jp.add(jb1);jp.add(jb2);

add(jp,BorderLayout.CENTER);

setSize(300,?300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setVisible(true);

}

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

new?Calculator();

}

@Override

public?void?actionPerformed(ActionEvent?ae)?{

if(ae.getSource()==jb1){

String?c=((String)?jcb.getSelectedItem()).trim();

if(jtf1.getText().trim().equals("")){

JOptionPane.showMessageDialog(this,?"第一個數(shù)不能為空");

jtf1.requestFocus();

return;

}

if(jtf2.getText().trim().equals("")){

JOptionPane.showMessageDialog(this,?"第二個數(shù)不能為空");

jtf2.requestFocus();

return;

}

double?num1=Double.parseDouble(jtf1.getText().trim());

double?num2=Double.parseDouble(jtf2.getText().trim());

double?num3?=?0;

switch(c){

case?"+":num3=num1+num2;break;

case?"-":num3=num1-num2;break;

case?"*":num3=num1*num2;break;

case?"/":

if(num2==0){

JOptionPane.showMessageDialog(this,?"除數(shù)不能為0");

jtf2.requestFocus();

return;

}else{

num3=num1/num2;

break;

}

}

jtf3.setText(String.valueOf(num3));

}

if(ae.getSource()==jb2){

System.exit(0);

}

}

}

運行結(jié)果

java簡單加減乘除

使用BigDecimal并且一定要用String來夠造。

實現(xiàn)方法如下:

import java.math.BigDecimal;

/**

* 由于Java的簡單類型不能夠精確的對浮點數(shù)進行運算,這個工具類提供精

* 確的浮點數(shù)運算,包括加減乘除和四舍五入。

*/

public class Arith{

//默認除法運算精度

private static final int DEF_DIV_SCALE = 10;

//這個類不能實例化

private Arith(){

}

/**

* 提供精確的加法運算。

* @param v1 被加數(shù)

* @param v2 加數(shù)

* @return 兩個參數(shù)的和

*/

public static double add(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.add(b2).doubleValue();

}

/**

* 提供精確的減法運算。

* @param v1 被減數(shù)

* @param v2 減數(shù)

* @return 兩個參數(shù)的差

*/

public static double sub(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.subtract(b2).doubleValue();

}

/**

* 提供精確的乘法運算。

* @param v1 被乘數(shù)

* @param v2 乘數(shù)

* @return 兩個參數(shù)的積

*/

public static double mul(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.multiply(b2).doubleValue();

}

/**

* 提供(相對)精確的除法運算,當(dāng)發(fā)生除不盡的情況時,精確到

* 小數(shù)點以后10位,以后的數(shù)字四舍五入。

* @param v1 被除數(shù)

* @param v2 除數(shù)

* @return 兩個參數(shù)的商

*/

public static double div(double v1,double v2){

return div(v1,v2,DEF_DIV_SCALE);

}

/**

* 提供(相對)精確的除法運算。當(dāng)發(fā)生除不盡的情況時,由scale參數(shù)指

* 定精度,以后的數(shù)字四舍五入。

* @param v1 被除數(shù)

* @param v2 除數(shù)

* @param scale 表示表示需要精確到小數(shù)點以后幾位。

* @return 兩個參數(shù)的商

*/

public static double div(double v1,double v2,int scale){

if(scale0){

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供精確的小數(shù)位四舍五入處理。

* @param v 需要四舍五入的數(shù)字

* @param scale 小數(shù)點后保留幾位

* @return 四舍五入后的結(jié)果

*/

public static double round(double v,int scale){

if(scale0){

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(Double.toString(v));

BigDecimal one = new BigDecimal("1");

return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

};

分享

本回答由電腦網(wǎng)絡(luò)分類達人 高德寶認證

其他類似問題

2009-02-08加減乘除運算(Java)14

2010-06-22Java實現(xiàn)有界面的簡單的兩個整數(shù)之間的加減乘除運算

2009-09-21myeclipse中java的加減乘除運算1

2011-05-27用java編寫計算器,,,要求加減乘除等運算。。。1

2011-09-15java中的String型怎么實現(xiàn)數(shù)學(xué)的加減乘除運算?10

更多關(guān)于java簡單加減乘除的問題

為您推薦:

舉報| 2009-02-24 03:00

#為什么很多走失的孩子無法找回?#

提問者采納

import java.util.Scanner;

class MakeOperation

{

public static void main(String[] args){

Scanner input=new Scanner(System.in);

Count count=new Count();

System.out.print("請輸入你要運算的第一個數(shù):");

count.one=input.next();

System.out.print("請輸入你要進行運算的運算符:");

count.fu=input.next();

System.out.print("請輸入你要運算的第二個數(shù):");

count.two=input.next();

if(count.fu.equals("+")){

count.outcome=Float.parseFloat(count.one)+Float.parseFloat(count.two)+"";

// 將字符串解析為數(shù)字,注意下如果輸入的不是數(shù)字程序會報錯,缺少健壯性代碼 }

System.out.println(count);

}

}

class Count{

String one;

String two;

String fu;

String outcome; // 結(jié)果

public String toString(){

return one+fu+two+"="+outcome;

}

}

下面的代碼修改了Count類里域的部分參數(shù),增加了一個用于檢查輸入是否正確的類。

import java.util.Scanner;

class MakeOperation

{

public static void main(String[] args){

Scanner input=new Scanner(System.in);

Count count=new Count();

CheckOperation checkOpera=new CheckOperation();

System.out.print("請輸入你要運算的第一個數(shù):");

count.one=checkOpera.checkNum(input);

System.out.print("請輸入你要進行運算的運算符:");

String[] operators={"+","-","*","/"};

count.fu=checkOpera.checkOperator(input,operators);

System.out.print("請輸入你要運算的第二個數(shù):");

count.two=checkOpera.checkNum(input);

if("+".equals(count.fu)){

count.outcome=count.one+count.two;

System.out.println(count);

}

}

}

class Count{

float one;

float two;

String fu;

float outcome; // 結(jié)果

public String toString(){

return one+fu+two+"="+outcome;

}

}

class CheckOperation{

/*驗證數(shù)字,輸錯了會提示用戶重新輸入*/

public float checkNum(Scanner input){

float num=0f;

try {

num=Float.parseFloat(input.next());

} catch (NumberFormatException e) {

System.out.print("請輸入正確的數(shù)字: ");

num=checkNum(input); //直至獲取到正確的值

}

return num;

}

/*驗證輸入的符號是否正確*/

public String checkOperator(Scanner input,String[] operators){

String opera=null;

boolean judge=true;

opera=input.next();

for(String operator:operators){

if(operator.equals(opera)) judge=false;

}

if(judge) {

System.out.print("請輸入正確的運算符: ");

opera=checkOperator(input,operators);

}

return opera;

}

}

網(wǎng)站名稱:用java代碼寫加減乘除,java加減乘除代碼怎么寫
網(wǎng)頁鏈接:http://chinadenli.net/article40/hesiho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)面包屑導(dǎo)航、自適應(yīng)網(wǎng)站營銷型網(wǎng)站建設(shè)、ChatGPT網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)