不可傳回直接顯示Sum
------------------------------------------
class test13
{
public static void main(String[] args)
{
Sum(1,100);
Sum(5,99);
}
static void Sum(int vStart,int vEnd)
{
int total=0;
for(int i=vStart;i<=vEnd;i++)
total+=i;
System.out.println(vStart+"加到"+vEnd+"的總和為"+total+"\n");
}
}
------------------------------------------

可傳回
------------------------------------------
class test14
{
public static void main(String[] args)
{
int tol1,tol2;
tol1=Sum(1,100);
System.out.println("1加到100總和為"+tol1+"\n");
tol2=Sum(5,99);
System.out.println("5加到99總和為"+tol2+"\n");
}
static int Sum(int vStart,int vEnd)
{
int total=0;
for(int i=vStart;i<=vEnd;i++)
total+=i;

return total;

}
}
------------------------------------------
arrow
arrow
    全站熱搜

    Neo Chao 發表在 痞客邦 留言(0) 人氣()