public class Sum{
public static void main(String args[]){
int sum = 0;
for(int i = 1 ;i<=100 ; i ++){
sum += i;
}
System.out.println("1+2+3+…+100="+sum);
}
}
public class Test {
public static void main(String[] args) {
int sum = 0;
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= 100; i++){
sum = sum + i;
sb.append(i + "+");
}
System.out.println(sb.substring(0, sb.length() -1).toString() + " = "+ sum);
}
}
public static void main(String[] args) {
int sum = 0;
String str = "";
for(int i = 1;i<101;i++){
sum += i;
if(i !=1){
str += "+"+String.valueOf(i);
}else {
str +=String.valueOf(i);
}
}
System.out.println(str+"="+sum);
}
循环语句
long sum=0;
for(int i=1;i<=100;i++){
sum=sum+i;
}
System.out,println("1+2+3+...+99+100="+sum);