- Math.round(11.5) 等于:()
A 11
B 11.5
C 12
D 12.5
【解析】round是跟取整有关的函数
public static void main(String[] args) {
System.out.println("小数点后第一位=5");
System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));//12
System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));//-11
System.out.println("小数点后第一位<5");
System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));//11
System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));//-11
System.out.println("小数点后第一位>5");
System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));//12
System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));//-12
}
规则:四舍五入,等于5时加上0.5后,向下取整。
Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应,例如:ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.6)的结果为-11;
floor的英文是地板,该方法就表示向下取整,Math.floor(11.6)的结果是11
Math.floor(-11.4)的结果-12;
round方法,表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果是12,Math.round(-11.5)的结果为-11。
2. 以下 _____ 不是 Object 类的方法
A clone()
B finalize()
C toString()
D hasNext()
【解析】Object() 中的方法有:clone() equals() finalize() getClass() hashCode() notify() notifyAll() toString() wait()
- Java采用的字符集编码——Unicode(标准码) UTF-8 是目前互联网上使用最广泛的一种 Unicode 编码方式.
- 【最小公倍数】
求两个数的最小公倍数,两个数的最小公倍数为:能被这两个数同时整除的最小的数。
最小公倍数= 两数乘积除以最大公约数(两数相乘防止过大越界,一般是颠倒一下顺序,可以先a/最大公约数*b);最大公约数用了辗转相除法
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int ret = min(a,b);
System.out.println(ret);
}
public static int max(int a,int b){
while(b != 0){
int tmp = a%b;
a=b;
b=tmp;
}
return a;
}
public static int min(int a, int b){
return a/max(a,b)*b;//防止a*b出界
}