首页 > 编程知识 正文

java实现大整数的四则运算(java设计一个能进行四则运算的计算器)

时间:2023-12-24 12:05:46 阅读:320899 作者:VQTS

本文目录一览:

JAVA中long型代码,支持大整数的四则运算

public long add(long a , long b){

BigInteger bigIntA = new BigInteger(a + "");

BigInteger bigIntB = new BigInteger(b + "");

return bigIntA.add(bigIntB).longValue;

}public long subtract(long a , long b){

BigInteger bigIntA = new BigInteger(a + "");

BigInteger bigIntB = new BigInteger(b + "");

return bigIntA.subtract(bigIntB).longValue;

}public long multiply(long a , long b){

BigInteger bigIntA = new BigInteger(a + "");

BigInteger bigIntB = new BigInteger(b + "");

return bigIntA.multiply(bigIntB).longValue;

}public long divide(long a , long b){

BigInteger bigIntA = new BigInteger(a + "");

BigInteger bigIntB = new BigInteger(b + "");

return bigIntA.divide(bigIntB).longValue;

}

运用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程序

import java.text.DecimalFormat;

import java.util.Scanner;

public class Zhidao {

public static void main(String[] args) {

String condition = "";

Zhidao zhidao = new Zhidao();

do{

Scanner scanner = new Scanner(System.in);

try{

System.out.print("请输入第一个数:");

double x = scanner.nextDouble();

System.out.print("请输入第二个数:");

double y = scanner.nextDouble();

System.out.print("请输入运算符:");

String s = scanner.next();

char z = s.charAt(0);

zhidao.yunsuan(x, y, z);

}catch(Exception e){

System.out.println("请输入正确的数据!");

}

System.out.print("是否继续?continue:继续,任意字符:结束");

condition = scanner.next();

}while("continue".equals(condition));

}

public static void yunsuan(double x,double y,Character z){

DecimalFormat r=new DecimalFormat();

r.applyPattern("#0.00");

if(z.equals('+')){

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

} else if(z.equals('-')){

System.out.println(x+"-"+y+"=" + r.format((x-y)));

} else if(z.equals('*')){

System.out.println(x+"*"+y+"=" + r.format((x*y)));

} else if(z.equals('/')){

if(y==0){

System.out.println("被除数不能为0");

} else{

System.out.println(x+"/"+y+"=" + r.format((x/y)));

}

}else{

System.out.println("无法识别改运算符");

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。