Failed to fetch (https://www.bing.com/turing/conversation/create)
时间: 2023-12-15 20:14:01 浏览: 142
1.创建一个Arithmetic类
public class Arithmetic {
//整数加法
public int add(int a, int b) {
return a+b;
}
//整数减法
public int subtract(int a, int b) {
return a-b;
}
//整数乘法
public int multiply(int a, int b) {
return a*b;
}
//整数除法
public int divide(int a, int b) {
return a/b;
}
//浮点数加法
public double add(double a, double b) {
return a+b;
}
//浮点数减法
public double subtract(double a, double b) {
return a-b;
}
//浮点数乘法
public double multiply(double a, double b) {
return a*b;
}
//浮点数除法
public double divide(double a, double b) {
return a/b;
}
//整数加法,多参数
public int add(int... nums) {
int sum = 0;
for(int num : nums) {
sum += num;
}
return sum;
}
//浮点数加法,多参数
public double add(double... nums) {
double sum = 0;
for(double num : nums) {
sum += num;
}
return sum;
}
}
2.测试类
public class TestArithmetic {
public static void main(String[] args) {
Arithmetic arithmetic = new Arithmetic();
//整数加法
System.out.println(arithmetic.add(1, 2));
//整数减法
System.out.println(arithmetic.subtract(2, 1));
//整数乘法
System.out.println(arithmetic.multiply(2, 3));
//整数除法
System.out.println(arithmetic.divide(6, 2));
//浮点数加法
System.out.println(arithmetic.add(1.2, 2.3));
//浮点数减法
System.out.println(arithmetic.subtract(2.3, 1.2));
//浮点数乘法
System.out.println(arithmetic.multiply(2.0, 3.0));
//浮点数除法
System.out.println(arithmetic.divide(6.0, 2.0));
//整数加法,多参数
System.out.println(arithmetic.add(1, 2, 3));
//浮点数加法,多参数
System.out.println(arithmetic.add(1.2, 2.3, 3.4));
}
}
阅读全文