/*题目:计算在-10.8到5.9之间,绝对值大于6或小于2.1的整数有多少个 * 分析:1.既然已经确定了范围,for循环 * 2.起点位置-10.8应该转换为-10(向上取整,向下取整,强制类型转换) * 3.每一个数字都是整数,所以步进表达式应该是num++,每次都是+1的 * 4.如何拿到绝对值:Math.abs方法。 * 5.一旦发现一个数字,需要让计数器++进行统计*/ public class MathPractiseOne { public static void main(String[] args) { int count=0;//统计 count++; double min=-10.8; double max=5.9; for(double i=min;i<max;i++){ double one=Math.floor(i);//向下取值 if(Math.abs(one)>6||Math.abs(one)<2){//绝对值 System.out.println(one); count++; } } System.out.println(count);//9 }}
Java作业:计算在-10.8到5.9之间,绝对值大于6或小于2.1的整数有多少个
最新推荐文章于 2021-11-08 10:06:35 发布