
正文
java乘法运算代码 java代码乘法表
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java的加减乘除运算
使用BigDecimal并且一定要用String来够造。
实现方法如下:
import java.math.BigDecimal;
/**
* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
* 确的浮点数运算,包括加减乘除和四舍五入。
*/
public class Arith{
//默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
//这个类不能实例化
private Arith(){
}
/**
* 提供精确的加法运算。
* @param v1 被加数
* @param v2 加数
* @return 两个参数的和
*/
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 被减数
* @param v2 减数
* @return 两个参数的差
*/
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 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
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();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后10位,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @return 两个参数的商
*/
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
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();
}
/**
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
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();
}
};
相关问答
Q1: java加减乘除运算的程序怎么编啊
//JAVA编程:四则运算(接收用户输入的2个操作数java乘法运算代码,和运算符),计算之后,输出结果~~~~
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("运算符是无意义字符java乘法运算代码!");
break;
}
}
}
望采纳~~~~~~~~
Q2: java 怎么算乘法
package CMJqimo;
import java.util.Random;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test {
static int trueresult = 0;
public static void main(String args[]) {
new test();
}
public test() {
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jtf;
JFrame jf = new JFrame("Exam of Multiplication");
JButton jb = new JButton();
Container contentPane = jf.getContentPane();
contentPane.add(jp);
contentPane.setLayout(new BorderLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 200);
jp.setLayout(new FlowLayout());
jp.setBackground(Color.GREEN);
jp.setSize(1000, 1000);
int num1 = random_number();
int num2 = random_number();
trueresult = num1 * num2;
JLabel jll = new JLabel(num1 + " x " + num2 + " =");
jp.add(jll);
jtf = new JTextField(5);
jp.add(jtf);
jb = new JButton("提交");
jp.add(jb);
jl = new JLabel(" ");
jp.add(jl);
contentPane.add(jp);
jf.setLocation(400, 200);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int input = Integer.valueOf(jtf.getText());
String s = output_result(trueresult, input);
JOptionPane.showMessageDialog(jp, s);
int a = random_number();
int b = random_number();
trueresult = a * b;
jll.setText(a + " x " + b + " =");
jtf.setText("");
}
});
}
public static int read_input(String s) {
return Integer.parseInt(s);
};
public static int random_number() {
Random r = new Random();
int num = r.nextInt(10) + 1;
return num;
}
public String output_result(int trueresult, int input) {
Random r = new Random();
if (input == trueresult) {
String[] s = { "Very good", "Excellent", "Great job" };
return s[r.nextInt(3)];
} else {
return "No, please try again";
}
}
}
Q3: 编写程序来执行两个数字12和5的算术加法,乘法和除法(java程序)求源代码
楼上的除法运算出错,a
/
b取商,a%b取余,同数学运算是有差异的。还有就是双精度浮点数的加减乘除会损失精度。建议用BigDecimal提供的方法来运算。
如:
BigDecimal
a
=
new
BigDecimal(12);
BigDecimal
b
=
new
BigDecimal(5);
加法运算:a=a.add(b);
减法运算:a=a.subtract(b);
乘法运算:a=a.multiply(b);
除法运算:a=a.divide(b);
获取运算结果:a.toString()
,
a.longValue()
,
a.intValue()
.....等等
保留两位小数,四舍五入:a=a.divide(b).setScale(2,
BigDecimal.ROUND_HALF_UP);
大量方法建议自查api文档。
java乘法运算代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码乘法表、java乘法运算代码的信息别忘了在本站进行查找喔。







