class Count02{ //计算1-100的和 public int getSum(){ int sum=0;//用于收集1-100的值 for (int i=1;i<=100;i++){ sum+=i; } return sum; } } public class Method_Exer03 { public static void main(String[] args) { Count02 c=new Count02(); //调用函数 计算和 int sum= c.getSum(); System.out.println("1-100的和是:"+sum); }