/** *项目:用Java编译实现1.1+2.2+3.3+······+10.10+11.11······99.99+100.100 * 时间:2019/8/8 * 作者:郑翰林 */ //方法一: public class Exercies7 { public static void main(String[] args) { double sum1=0.0; double sum2=0.0; double sum3=0.0; for(double a=1.1;a<=9.9;a+=1.1){ sum1+=a; } for(double b=10.10;b<=100;b+=1.01){ sum1+=b; } sum1+=100; System.out.println("sum1="+sum1); //方法二: for(int d=1;d<=100;d++){ String a=String.valueOf(d)+"."+String.valueOf(d); double b=Double.parseDouble(a); sum2+=b; } System.out.println("sum2="+sum2); //方法三: for(int c=1;c<=100;c++){ sum3+=c; if (c<=9){ sum3+=c/10.0; }else if(c>=10&&c<=99){ sum3+=c/100.0; } else{ sum3+=c/1000.0; } } System.out.println("sum3="+sum3); } }
用Java编译实现1.1+2.2+3.3+······+10.10+11.11······99.99+100.100
最新推荐文章于 2022-06-23 10:12:12 发布