📄 sumofarray.java
字号:
package exercise_java;
public class SumOfArray {
public static void main(String[] args){
final int NUMBER=2009;
int num1,num2,sum=0;
int i=1;
do{
sum+=i;
i++;
}while(sum<=NUMBER);
num1=sum;
num2=sum-i;
if(Math.abs(num1-NUMBER)<Math.abs(num2-NUMBER))
System.out.println("the most approximate number is "
+i+" the sum of 1+2+3+...is "+num1);
else if ((Math.abs(num1-NUMBER)<Math.abs(num2-NUMBER)))
System.out.println("the most approximate number is "
+(i-1)+" the sum of 1+2+3+...is "+num2);
else
System.out.println("the most approximate number is "
+i+" or "+(i-1)+" the sum of 1+2+3+...is "+num1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -