网站建设资讯

NEWS

网站建设资讯

java网页加减乘除代码,java加减乘除代码怎么写

求简单java写计算器代码加减乘除

import javax.swing.*;

创新互联公司服务项目包括饶平网站建设、饶平网站制作、饶平网页制作以及饶平网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,饶平网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到饶平省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

import java.awt.*;

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener

{

private boolean dotExist, operated, equaled; // 帮助运算的布尔变量

private double storedNumber; // 目前的结果

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

private JTextField operation; // 结果栏

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

private JButton[] numbers; // 数字

// 构造者

public Calculator()

{

setTitle("Calculator");

// 初始化变量

dotExist = false; // 表示当前的数是否有小数点

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);

}

// 对按钮进行反应的方法

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语言在网页里实现加减乘除算法的源代码要怎么写吖?!

public class Excer{

public static void main(String args[])

{

Excer ex=new Excer();

int x=0;

int y=0;

ex.math(x,y);

}

void math(int x,int y){

MyMath mm = new MyMath();

System.out.println("x="+x+" ,y="+y);

System.out.println("x+y="+mm.plus(x,y));

System.out.println("x-y="+mm.minus(x,y));

System.out.println("x*y="+mm.multi(x,y));

System.out.println("x/y="+mm.div(x,y));

}

}

class MyMath

{

int plus(int a,int b)

{

return(a+b);

}

int minus(int a,int b)

{

return(a-b);

}

int multi(int a,int b)

{

return(a*b);

}

float div(int a,int b)

{

return ((float)a/b);

}

}

求:Java 计算器加减乘除代码 注释

public class MyComputer implements ActionListener{

private boolean appendnumber = false;

private boolean append = false;

private boolean flag = false;

private String temp1 = null;

private String op1 = null;

private JTextField jtf = new JTextField("0.",28);

public MyComputer(){

init();

}

private void init() {

/*

* 下面都是在画计算机界面你想看也可以,

* 不看也行,具体计算在public void

* actionPerformed(ActionEvent e)

* 里

*/

JFrame frame = new JFrame("我的计算器");

JPanel panelTop = new JPanel();

panelTop.setLayout(new FlowLayout());

panelTop.add(jtf);

JPanel panelBotton = new JPanel();

String[] str = {"BackSpace","CE","C","+",

"7","8","9","-",

"4","5","6","*",

"1","2","3","/",

"0","+/-",".","="};

JButton[] jb = new JButton[str.length];

panelBotton.setLayout(new GridLayout(5,4));

for(int i = 0;i jb.length;i++)

{

jb[i] = new JButton(str[i]);

panelBotton.add(jb[i]);

jb[i].addActionListener(this);

}

frame.add(panelTop,BorderLayout.NORTH);

frame.add(panelBotton);

frame.setSize(400, 400);

frame.setVisible(true);

}

public static void main(String[] args) {

new MyComputer();

}

public void actionPerformed(ActionEvent e) {

String comm = e.getActionCommand();//获得button里的数据

/*

* "0123456789".indexOf(comm)!=-1是判断button里获得的数据

* 是0---9的情况

*/

if ("0123456789".indexOf(comm)!=-1) {

if(!appendnumber){

jtf.setText(comm);

appendnumber = true;

}else{

jtf.setText(jtf.getText()+comm);

}

}

/*

* 判断button里获得的数据是加减乘除的情况

*/

if("+-*/".indexOf(comm)!=-1){

//在遇到加减乘除的符号时我们先存储刚输入的值

temp1 = jtf.getText();

//然后存储符号

op1 = comm;

/*

* 然后可以重新输入数据到输入框,所以这时候我们

* 不能追加数据,设置追加数据标志为false

*/

appendnumber = false;

}

/*

* 判断button里获得的数据是等号的情况

*/

else if("=".equals(comm)) {

if(temp1==null||"".equals(temp1))return;

/*

* 遇到等号的时候,我们取出刚才保存的数据和当前输入的数据做运算

*/

double num1 = Double.parseDouble(temp1);//转化数据成double型的

double num2 = Double.parseDouble(jtf.getText());//转化数据成double型的

//取出刚才保存的符号看是加减乘除的那种运算

if("+".equals(op1)){

num1+=num2;

}

if("-".equals(op1)){

num1-=num2;

}

if ("*".equals(op1)) {

num1*=num2;

}

if("/".equals(op1)){

num1/=num2;

}

jtf.setText(num1+"");

appendnumber = false;

}

/*

* 判断button里获得的数据是小数点的情况

*/

else if (".".equals(comm)) {

if(jtf.getText().indexOf(".")==-1){

append = true;

}

if (append) {

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

append = false;

appendnumber = true;

}

}

/*

* 判断button里获得的数据是在数据前面加正负号的情况

*/

else if ("+/-".equals(comm)) {

if(!flag){

jtf.setText("-"+jtf.getText());

flag = true;

}

else{

String result = jtf.getText().substring(1, jtf.getText().length());

jtf.setText(result);

flag = false;

}

}

/*

* 判断button里获得的数据是清除输入框的情况

*/

else if ("C".equals(comm)) {

jtf.setText("0.");

appendnumber = false;

}

/*

* 判断button里获得的数据是清除输入框的情况

*/

else if ("CE".equals(comm)) {

jtf.setText("0.");

appendnumber = false;

}

/*

* 判断button里获得的数据是输入框的数据退一位的情况

*/

else if ("BackSpace".equals(comm)) {

if(jtf.getText()==null||"".equals(jtf.getText()))return;

String result = jtf.getText().substring(0, jtf.getText().length()-1);

jtf.setText(result+"");

appendnumber = true;

}

}

}

很简单的东西啊!你们去分那么多情况啊!这个东西啊一个人写半个小时就OK了,如果你会写的话,其实初学者们别因为别人怎么怎么说就吓倒,其实这写起来都没多少代码的,都是些小东西而已。最重要的是你要搞懂来龙去脉就不难了,别听一些人吹谁谁牛X这类的,其实你认真的去做,认真的去思考,你也不比他们差。

怎么用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编写实现加减乘除,界面如下

用Java编写的实现加减乘除的程序如下

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("第一个数");

JLabel jl2=new JLabel("运算符");

JLabel jl3=new JLabel("第二个数");

JLabel jl4=new JLabel("结果:");

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, "第一个数不能为空");

jtf1.requestFocus();

return;

}

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

JOptionPane.showMessageDialog(this, "第二个数不能为空");

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, "除数不能为0");

jtf2.requestFocus();

return;

}else{

num3=num1/num2;

break;

}

}

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

}

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

System.exit(0);

}

}

}

运行结果

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网页加减乘除代码,java加减乘除代码怎么写
标题链接:http://njwzjz.com/article/dseepih.html