1、打開(kāi)eclipse,創(chuàng)建一個(gè)Java工程,在此工程里新建一個(gè)類;

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供聞喜企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、html5、小程序制作等業(yè)務(wù)。10年已為聞喜眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
2、在新建的類中,添加4個(gè)運(yùn)算類;
3、在主方法中調(diào)用對(duì)應(yīng)的方法即可完成分?jǐn)?shù)的加減乘除運(yùn)算了。
具體實(shí)現(xiàn)代碼如下:
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));
}
//加法運(yùn)算
private static float jia(float x,float y) {
return x + y;
}
//減法運(yùn)算
private static float jian(float x,float y) {
return x - y;
}
//乘法運(yùn)算
private static float cheng(float x,float y) {
return x * y;
}
//除法運(yùn)算
private static float chu(float x,float y) {
return x / y;
}
}
根據(jù)你的要求,我寫(xiě)了一下代碼,如下:package com.fer2005.test;public class AddUtil { /**
* @param a1 大數(shù)字1,按數(shù)組存儲(chǔ)
* @param a2 大數(shù)字2,按數(shù)組存儲(chǔ)
* @return 返回結(jié)果數(shù)組
*/
public int[] getResult(int[] a1,int[] a2){
//取最大的長(zhǎng)度作為返回結(jié)果的長(zhǎng)度,此時(shí)未考慮是否有進(jìn)位
int length=a1.lengtha2.length?a1.length:a2.length;
//新建未考慮進(jìn)位的數(shù)組結(jié)果
int[] tmp_res =new int[length];
int i=0;
//循環(huán)相加得到res的按照最短數(shù)組相加的結(jié)果
while(ia1.lengthia2.length){
i++;
tmp_res[length-i]=a1[a1.length-i]+a2[a2.length-i];
}
//操作完成后,需將長(zhǎng)數(shù)組的值賦給res
//a1的長(zhǎng)度說(shuō)明a1比a2長(zhǎng)度小,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){
//說(shuō)明a2比a1長(zhǎng)度小,res需要獲取a1的前幾位
while(length-i0){
tmp_res[length-i-1]=a1[a1.length-i-1];
i++;
}
} //考慮進(jìn)位問(wèn)題,如果某一元素大于10,則本身減10,前一元素加1,如果第一個(gè)元素大于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+",");
}
}}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 幫助運(yùn)算的布爾變量
private double storedNumber; // 目前的結(jié)果
private char lastOperator; // 表示上一運(yùn)算符
private JTextField operation; // 結(jié)果欄
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 運(yùn)算符
private JButton[] numbers; // 數(shù)字
// 構(gòu)造者
public Calculator()
{
setTitle("Calculator");
// 初始化變量
dotExist = false; // 表示當(dāng)前的數(shù)是否有小數(shù)點(diǎn)
operated = false; // 表示任意運(yùn)算符是否被按下
equaled = false; // 表示等號(hào)是否被按下
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);
}
// 對(duì)按鈕進(jìn)行反應(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;
}
}
}
// 進(jìn)行運(yùn)算的方法
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編寫(xiě)的實(shí)現(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("第一個(gè)數(shù)");
JLabel?jl2=new?JLabel("運(yùn)算符");
JLabel?jl3=new?JLabel("第二個(gè)數(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("計(jì)算");
JButton?jb2=new?JButton("退出");
JPanel?jp=new?JPanel();
Calculator(){
setTitle("計(jì)算器");
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,?"第一個(gè)數(shù)不能為空");
jtf1.requestFocus();
return;
}
if(jtf2.getText().trim().equals("")){
JOptionPane.showMessageDialog(this,?"第二個(gè)數(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);
}
}
}
運(yùn)行結(jié)果
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;
}
}
當(dāng)前文章:大數(shù)加減乘除java代碼 java編程加減乘除
網(wǎng)站網(wǎng)址:http://chinadenli.net/article18/hgpidp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、做網(wǎng)站、App設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)