import java.util.Math;
/**
* @author : sunkepeng E-mail:[email protected]
* @date : 2020/7/21 11:48
*
* 题目:
* 计算在-10.8到5.9范围中,绝对值大于6或者小于2.1的整数的个数
* 分析:
* 1.有范围--->用for循环
* 2.起点位置-10.8应该转换成-10
* 2.1可以使用Math.ceil方法,向上取整
* 2.2可以强制转换成int,自动舍弃小数位
* 3.每一个数字都是整数,所以步进表达式应该是num++
* 4.一旦发现一个数字,需要计数器++统计
*
* 备注:如果使用ceil,获得是double类型的值,如10.0,double值也可以进行 ++ 操作
*/
public class MathTest {
public static void main(String[] args) {
int count = 0;
double min = -10.8;
double max = 5.9;
// 区间之内所有的整数
for (int i = (int) min; i < max; i++) {
int abs = Math.abs(i);
if (abs > 6 || abs < 2.1) {
System.out.println(i);
count++;
}
}
System.out.println("总共有:" + count);
}
}
Java使用Math类来计算绝对值在一定范围内的整数个数demo
猜你喜欢
转载自blog.csdn.net/kepengs/article/details/107485741
今日推荐
周排行