成都网站建设设计

将想法与焦点和您一起共享

大数加减乘除java代码 java编程加减乘除

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

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

成都创新互联公司是一家专业提供闻喜企业网站建设,专注与网站设计、成都网站制作html5、小程序制作等业务。10年已为闻喜众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

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实现两个无限大的数的加减乘除运算

根据你的要求,我写了一下代码,如下:package com.fer2005.test;public class AddUtil { /**

* @param a1 大数字1,按数组存储

* @param a2 大数字2,按数组存储

* @return 返回结果数组

*/

public int[] getResult(int[] a1,int[] a2){

//取最大的长度作为返回结果的长度,此时未考虑是否有进位

int length=a1.lengtha2.length?a1.length:a2.length;

//新建未考虑进位的数组结果

int[] tmp_res =new int[length];

int i=0;

//循环相加得到res的按照最短数组相加的结果

while(ia1.lengthia2.length){

i++;

tmp_res[length-i]=a1[a1.length-i]+a2[a2.length-i];

}

//操作完成后,需将长数组的值赋给res

//a1的长度说明a1比a2长度小,res需要获取a2的前几位

if(a1.lengtha2.length){

while(length-i0){

tmp_res[length-i-1]=a2[a2.length-i-1];

i++;

}

}else if(a1.lengtha2.length){

//说明a2比a1长度小,res需要获取a1的前几位

while(length-i0){

tmp_res[length-i-1]=a1[a1.length-i-1];

i++;

}

} //考虑进位问题,如果某一元素大于10,则本身减10,前一元素加1,如果第一个元素大于10,特殊处理。

//需处理相加之和大于10的情况

for(int k=tmp_res.length-1;k0;k--){

if(tmp_res[k]=10){

tmp_res[k-1]=tmp_res[k-1]+1;

tmp_res[k]=tmp_res[k]-10;

}

}

int[] res=new int[length+1];

//首位情况特殊处理

if(tmp_res[0]=10){

res[0]=1;

res[1]=tmp_res[0]-10;

for(int m=1;mtmp_res.length;m++){

res[m+1]=tmp_res[m];

}else{ res=tmp_res; }

}

return res;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

AddUtil addUtil = new AddUtil();

int[] a1= {9,2,3,8,5};

int[] a2={1,9,2,3,9};

for(int j:addUtil.getResult(a1, a2)){

System.out.print(j+",");

}

}}

求简单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编写实现加减乘除,界面如下

用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中大数类实现大数的四则运算

import java.math.BigInteger;

public class BigIntegerGet {

public String getAdd(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.add(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getSubtract(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.subtract(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getMultiply(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.multiply(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getDivide(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.divide(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

}


新闻标题:大数加减乘除java代码 java编程加减乘除
转载来于:http://chengdu.cdxwcx.cn/article/hgpidp.html