使用for循环完成不同步长的累加
package com.one;
public class ForStatement {
public static void main(String[] args) {
forStatementTest();
}
public static void forStatementTest(){
int tempN = 10;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
tempN = 0;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
int tempStepLength = 1;
tempN = 10;
System.out.println("1 add to " + tempN +" with step length " +tempStepLength + " is: " + addToNWithStepLength(tempN,tempStepLength));
tempStepLength = 2;
tempN = 10;
System.out.println("1 add to " + tempN +" with step length " +tempStepLength + " is: " + addToNWithStepLength(tempN,tempStepLength));
}
public static int addToN(int paraN){
int resultSum = 0;
for(int i = 1 ; i<= paraN ; i++){
resultSum += i;
}
return resultSum;
}
public static int addToNWithStepLength(int paraN,int paraStepLength) {
int resultSum = 0;
for(int i = 1 ; i <= paraN ; i += paraStepLength)
{
resultSum += i;
}
return resultSum;
}
}
运行结果
